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

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

问题描述:

首先,我应该说我是MVC的新手,但是在项目中使用它的时候学习它。MVC部分视图将模型传递给控制器​​

我目前有一个问题,我认为源于我对MVC的理解不够。这里是什么,我有一个轮廓:

控制器: PersonController

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

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

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

任何帮助将不胜感激!

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

关于你的'被调用的一个方法和被按下的按钮的名字被确定',你不需要这个。每个按钮都可以在控制器上调用不同的操作。就个人而言,我会在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 
} 
+0

Th很多。我会审视你的建议并尝试并实施它。 – 2013-05-02 07:53:18