3

首先,我应该说我是 MVC 新手,但在项目中使用它时会学习它。

我目前有一个问题,我认为这源于我对 MVC 缺乏了解。这是我所拥有的概述:

控制器: 人控制器

模型: PersonIndexModel PersonFindAddressModel(邮政编码,可能的地址列表) PersonNewAddressModel(第一行,第二行,邮政编码)

视图: RegisterAddress(使用 PersonIndexModel,包括两个部分视图)。FindAddress(使用 PersonFindAddressModel) NewAddress(使用 PersonNewAddressModel)。

我打算发生的是,在注册时,用户输入邮政编码。我有一个 html 提交按钮,然后返回到控制器,检查按下按钮的名称,如果它是“查找邮政编码”,它将传回可能的地址列表(使用 PersonFindAddressModel)。

如果用户选择该地址不在列表中,它会再次回到与之前相同的位置,检查按下了哪个按钮(在本例中为“找不到地址”),隐藏“查找地址”部分,然后显示“新地址”部分。

新地址包含普通地址表格和提交按钮。我试图创建一个新的 ActionLink 来调用 PersonController 中的某些内容,但它不会。

我哪里错了?部分应该有控制器吗?

更具体地说,这是我的代码:

注册地址

@Html.Partial("FindAddress")
@Html.Partial("NewAddress", new PersonNewAddressModel())

查找地址

@model PersonFindAddressModel
@using (Html.BeginForm("RegisterAddress", "Person"))
{
if (@ViewBag.NewAddress != true)
{
    @Html.TextBoxFor(m=> m.PostCode)

    <button type="submit" value="findaddress" name="command">Find Address</button>


    if (Model != null && Model.AddressList != null && Model.AddressList.Count > 0)
    {
        <div class="selectproperty">
            @Html.DropDownList("selectaddress", new SelectList(Model.AddressList, "value", "text"))
        </div>

        <div>
            <button type="submit" value="notinlist" name="command"> Not in the list?</button>
        </div>
    }

}

新地址

@model PersonNewAddressModel

@{
    ViewBag.Title = "FindAddress";
}

@using (Html.BeginForm("RegisterAddress", "Person"))
{
 if (@ViewBag.NewAddress == true)
   {
    @Html.TextBoxFor(m=> m.Street)

    @Html.ActionLink("Submit", "SubmitNew")
}
}

个人控制器

public class PersonController : Controller
{
[HttpPost]
    public ActionResult RegisterAddress(PersonFindAddressModel model, string command)
    {
        switch (command)
        {
            case "findaddress":
                PostcodeLookup(model);
                break;

            case "notinlist":
                ViewData.Add("NewAddress", true);
                break;

        }

        return View(model);
    }

 public ActionResult SubmitNew(PersonNewAddressModel model)
    {

        return View(model);
    }

任何帮助将不胜感激!

4

1 回答 1

1

您可以对所有功能使用相同的控制器。您的局部不需要单独的控制器。

关于您的“调用一个方法并确定按下的按钮的名称”,您不需要这样做。每个按钮都可以在控制器上调用不同的操作。就个人而言,我会在 FindAddress 视图中使用两种形式:

@model PersonFindAddressModel
@using (Html.BeginForm("FindAddress", "Person"))
{
    if (@ViewBag.NewAddress != true)
    {
        @Html.TextBoxFor(m => m.PostCode)
        <input type="submit" value="Find" />
    }
}

if (Model.AddressList != null && Model.AddressList.Count > 0)
{
    @using (Html.BeginForm("SaveAddress", "Person")
    {
        <div class="selectproperty">
            @Html.DropDownList("selectaddress", new SelectList(Model.AddressList, "value", "text"))
        </div>

        <div>
            <input type="submit" value="Save" />
            @Html.ActionLink("Not in list","NotInList")
        </div>
    }
}

你对这些的行动:

public ActionResult FindAddress(PersonFindAddressModel model)
{
    var address = PostcodeLookup(model);

    // bind found address to PersonFindAddressModel

    return View(model);
}

public ActionResult NotInList()
{
    ViewData.Add("NewAddress", true);
    return RedirectResult("Index");
}

public ActionResult SaveAddress(PersonFindAddressModel model)
{
    // save address from model and do any redirecting ect.
}

对于 AddNewAddress 视图:

@model PersonNewAddressModel

@using (Html.BeginForm("SubmitNewAddress", "Person"))
{
    @Html.TextBoxFor(m => Model.Street)
    <input type="submit" value="Add New" />
}

然后为 SubmitNewAddress 添加一个操作:

public ActionResult SubmitNew(PersonNewAddressModel model)
{
    // save new address and redirect
}
于 2013-05-01T21:38:48.277 回答