asp.net-mvc - MVC 部分 View 将模型传递给 Controller

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

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

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

Controller : 个人 Controller

型号: 人物索引模型 PersonFindAddressModel(邮政编码,可能的地址列表) PersonNewAddressModel(第一行,第二行,后置码)

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

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

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

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

我哪里错了?部分应该有 Controller 吗?

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

注册地址

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

个人 Controller

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

如有任何帮助,我们将不胜感激!

最佳答案

您可以对所有功能使用同一个 Controller 。您不需要为您的部分单独的 Controller 。

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

@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 View :

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

关于asp.net-mvc - MVC 部分 View 将模型传递给 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16320646/

相关文章:

c# - ASP.NET MVC 路由/SEO 友好 URL

asp.net-mvc - 隐藏 Razor 输入上的 ng-model 不起作用

asp.net - 在 ASP.NET Core MVC 中嵌套 TagHelpers

部分 View 重新加载后,jQuery keyup 事件不起作用?

jquery - 使用 Ajax 更新 Asp.Net MVC 部分 View

jquery - 如何使用ajax调用将对象传递给 Controller

css - 英文数字在 asp 网站中显示为波斯语

c# - 如何使用 Moq 在 ASP.NET MVC 中模拟 HttpContext?

asp.net-mvc - 如何在 Razor 中强制执行代码块

c# - 使用 Ajax 不显眼地渲染部分 View