【问题标题】:Single strongly Typed Partial View for two similar classes of different types两个不同类型的相似类的单个强类型局部视图
【发布时间】:2013-05-02 17:48:20
【问题描述】:

我有一个注册主视图,其中显示了两种不同类型的地址 1. 家庭地址 2. 邮寄地址

  public class RegisterModel
     {             
        public AddressModel HomeAddress { get; set; }
        public AddressModel MailAddress { get; set; }
     }

public class AddressModel
{
   public string Street1 { get; set; }
   public string Street2 { get; set; }
   public string State   { get; set; }
   public string City    { get; set; }
}

我的主要注册视图强类型为 RegisterModel 如下

@model MyNamespace.Models.RegisterModel
@{
     Layout = "~/Views/_Layout.cshtml";
 }
@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "myForm" }))
{
  <div id="form">
    @Html.Action("MyAddressPartial")
    @Html.Action("MyAddressPartial")  
  </div>
}

MyAddressPartialView 如下:-

@model MyNamespace.Models.AddressModel
@{
    Layout = "~/Views/_Layout.cshtml";
}
 <div id="Address">
    @Html.TextBoxFor(m=>m.Street1 ,new { @id="Street1 "})     
    @Html.TextBoxFor(m=>m.Street2,new { @id="Street2"})
    @Html.TextBoxFor(m=>m.State ,new { @id="State "})
    @Html.TextBoxFor(m=>m.City,new { @id="City"})
 </div>

我的注册控制器:-

// Have to instantiate the strongly Typed partial view when my form first loads
// and then pass it as parameter to "Register" post action method. 
// As you can see the @Html.Action("MyAddressPartial") above in main    
// Register View calls this.
public ActionResult MyAddressPartial()
{
   return PartialView("MyAddressPartialView", new AddressModel());
}

我在同一个注册控制器中将我的主表单提交给下面提到的操作方法。

[HttpPost]
public ActionResult Register(RegisterModel model, 
                            AddressModel homeAddress, 
                            AddressModel mailingAddress)
{
       //I want to access homeAddress and mailingAddress contents which should 
       //be different, but as if now it comes same.
}

我不想为 MailingAddress 和 HomeAddress 创建一个单独的类。如果我这样做,那么我将不得不为每个地址创建两个单独的强类型部分视图。

关于如何重用类和局部视图并使它们动态化并在 Action Method Post 中读取它们的单独值的任何想法。

编辑 1 条对 scott-pascoe 的回复:-

在 DisplayTemplates 文件夹中,我添加了以下 AddressModel.cshtml

 <div>
        @Html.DisplayFor(m => m.Street1);
        @Html.DisplayFor(m => m.Street2);
        @Html.DisplayFor(m => m.State);
        @Html.DisplayFor(m => m.City);            
 </div>

还在 EditorTemplate 文件夹中,我添加了以下 AddressModel.cshtml 但使用 EditorFor

 <div>
     @Html.EditorFor(m => m.Street1);
     @Html.EditorFor(m => m.Street2);
     @Html.EditorFor(m => m.State);
     @Html.EditorFor(m => m.City);            
  </div>

现在我如何在 RegisterView 中使用它们以及如何在 Controller 的 post Action Method 中读取值?还有什么需要修改的?我在上面添加了几乎整个代码。我是 MVC 的初学者。

【问题讨论】:

    标签: asp.net-mvc partial-views html-helper strongly-typed-view


    【解决方案1】:

    执行此操作的典型 ASP.NET MVC 方法是将 EditorTemplates 和 DisplayTemplates 用于您的自定义类型。

    在 ~/Views/Shared 中,创建两个文件夹,DisplayTemplatesEditorTemplates。 在 DisplayTemplates 文件夹中创建一个带有模型名称的局部视图,即 (AddressModel),并创建一个 DisplayFor 模板。

    在 EditorTemplates 文件夹中创建另一个名为 AddressModel.cshtml 的局部视图并创建一个 EditorFor 模板。

    然后,MVC 将自动使用您的模板并为您提供所需的数据。

    【讨论】:

    • 请看我的编辑 1 对 scott-pascoe 的回复。请让我知道还需要做什么。
    • @marcind,请看看这个?
    • 谢谢..我现在意识到 EditorFor 的帮助。
    【解决方案2】:

    在您的视图中使用@Html.EditorFor(或@Html.DisplayFor,用于显示):

    @model MyNamespace.Models.RegisterModel
    @{
         Layout = "~/Views/_Layout.cshtml";
     }
     @using (Html.BeginForm(null, null, FormMethod.Post, new { id = "myForm" }))
     {
      <div id="form">
          @Html.EditorFor(m => m.HomeAddress)
          @Html.EditorFor(m => MailAddress)  
      </div>
     }
    

    您不需要为这些部件设置单独的控制器操作,只需在控制器之前填充 RegisterModel 中的地址即可。像这样:

    [HttpGet]
    public ActionResult Register() // this will be the page people see first
    {
        var model = new RegisterModel();
        return View(model);  // assuming your view is called Register.cshtml
    }
    
    
    [HttpPost]
    public ActionResult Register(RegisterModel model){
        DosomethingWithHomeAddress(model.HomeAddress);
        DosomethingWithMailAddress(model.MailAddress);
        model.IsSaved = true; // some way to let the user knwo that save was successful;
                              // if this is true, display a paragraph on the view
        return View(model);
    }
    

    【讨论】:

    • 感谢您的回答。我很傻,因为我告诉我我在 mvc 中很天真。我没有得到这部分 model.HomeAddress = GetAddress(...调用一个方法来获取家庭地址..);我只想将注册表单中的数据传递回 HttpPost 方法,那么我将在 HttpGet 的 GetAddress 中编写什么代码?
    • 哦,抱歉,我的错误,我认为如果您编辑它,您可能会从某个地方读取地址。如果你不这样做,你只需要创建模型。 :)
    • ,你说我只需要创建模型,我还是没听懂。你能修改你的答案吗?
    • 好的,我把令人困惑的行去掉了。
    • 那没有帮助。感谢您的尝试。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多