MVC部分视图将模型传递给控制器

时间:2013-05-01 15:02:27

标签: asp.net-mvc razor asp.net-mvc-partialview

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

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

控制器: PersonController

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

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

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

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

新地址包含普通地址表单和要提交的按钮。我试图创建一个新的ActionLink来调用PersonController中的东西,但它不会。

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

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

RegisterAddress

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

FindAddress

@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")
}
}

PersonController

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);
    }

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

您可以使用相同的控制器来执行所有功能。你的部分人不需要单独的控制器。

关于您正在调用的一种方法以及按下的按钮的名称被确定&#39;你不需要这个。每个按钮可以在Controller上调用不同的Action。就个人而言,我会在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
}