【问题标题】:MVC Partial Views Passing Model to ControllerMVC 部分视图将模型传递给控制器
【发布时间】:2013-05-01 15:02:26
【问题描述】:

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

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

控制器: 个人控制器

型号: 个人指数模型 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")
}
}

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

任何帮助将不胜感激!

【问题讨论】:

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


    【解决方案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
    }
    

    【讨论】:

    • 非常感谢。我会仔细研究您的建议并尝试实施。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-24
    • 1970-01-01
    • 1970-01-01
    • 2012-02-02
    相关资源
    最近更新 更多