C# MVC 4 Razor,带有部分视图的发布表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18420751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:10:02  来源:igfitidea点击:

MVC 4 Razor, Posting form with partial views

c#asp.netasp.net-mvcasp.net-mvc-4razor

提问by indranil pal

I am new to MVC 4 and razor. I have a view which contains multiple partial views. Due to the functionality of the partial views I am planning to reuse these in other views also.

我是 MVC 4 和剃刀的新手。我有一个包含多个局部视图的视图。由于部分视图的功能,我也计划在其他视图中重用它们。

My model is a collection of complex objects e.g:

我的模型是复杂对象的集合,例如:

    public class EmployeeInfo
    {
        public EmployeeContactInfo contactInfo { get; set; }
        public List<TelephoneInfo> phoneDetails { get; set; }
        public AddressDetails addressDetails { get; set; }
    }

The model of my main view is EmployeeInfoand other partial views have models as TelephoneInfo, EmployeeContactInfoand AddressDetailsrespectively.

我的主视图的模式是EmployeeInfo和其他部分的意见,有模型TelephoneInfoEmployeeContactInfoAddressDetails分别。

I tried using RenderPartial, RenderActionand Partialto load my partial views e.g:

我尝试使用RenderPartial,RenderActionPartial加载我的部分视图,例如:

    @using (Html.BeginForm())
    {
    @Html.Partial("ContactInfo",Model.contactInfo)
    }

When the main form is submitted the main model doesnt have the updated values of the partial views.

当提交主表单时,主模型没有局部视图的更新值。

I searched for this and found below 2 proposed solutions:

我搜索了这个并找到了以下 2 个建议的解决方案:

  1. Use EditorFor- It works and the model gets updated but I have collection of not only textbox but other controls which have some internal operations (like searching addresses) too and I also need to reuse the same partial view in other places (like a user control in classic ASP.NET)

  2. Use RenderActioninstead of RenderPartial- It didn't work for me.

  1. 使用EditorFor- 它可以工作并且模型得到更新,但我不仅收集了文本框,还收集了其他具有一些内部操作(如搜索地址)的控件,我还需要在其他地方重用相同的部分视图(如用户控件经典的 ASP.NET)

  2. 使用RenderAction而不是RenderPartial- 它对我不起作用。

Please let me know if I am going wrong or understood anything incorrectly.

如果我出错或理解有误,请告诉我。

采纳答案by Jason Evans

Another choice is to create an editor template. For example, in your main view:

另一种选择是创建一个编辑器模板。例如,在您的主视图中:

@using (Html.BeginForm())
{
    @(Html.EditorFor(m => m.ContactInfo))
}

Now, in your Views/Shared folder (or the Views/ControllerName folder eg Views/Home), create a new folder named "EditorTemplates". In that new folder create a cshtml view file named EmployeeContactInfo.cshtml(tip, the name of the cshtml should be the data type name e.g. string, boolor in this case your customer contact info type). In that view file, put something like:

现在,在您的 Views/Shared 文件夹(或 Views/ControllerName 文件夹,例如 Views/Home)中,创建一个名为“EditorTemplates”的新文件夹。在那个新文件夹中创建一个名为EmployeeContactInfo.cshtml(提示,cshtml 的名称应该是数据类型名称,例如stringbool或者在这种情况下您的客户联系信息类型)的 cshtml 视图文件。在该视图文件中,放置如下内容:

@model EmployeeContactInfo

@Html.LabelFor(m => m.Email)
@Html.TextBoxFor(m => m.Email)

When you post back to the controller, the values will be included as part of the returned model for you.

当您回发到控制器时,这些值将作为返回模型的一部分包含在内。

回答by Daniele

The problem is that your main form is expecting elements with the name contactInfo.property, probably in you partial the names you generate are property without the right identifiers, this issue create problems with the default model binder of MVC. You can create a custom model binder, but with hard work, or you have two ways to solve your issue with less work.

问题是您的主表单需要名称为 contactInfo.property 的元素,可能在您的部分中,您生成的名称是没有正确标识符的属性,这个问题会导致 MVC 的默认模型绑定器出现问题。您可以创建自定义模型绑定器,但需要努力工作,或者您有两种方法可以用更少的工作来解决您的问题。

1. Force the name of the generated element

1.强制生成元素的名字

For example, if your EmployeeContactInfoclass contains a string Addressproperty you have to use this helper:

例如,如果你的EmployeeContactInfo类包含一个字符串Address属性,你必须使用这个助手:

@Html.TextBox("contactInfo.Address", model.Address)

that results in:

结果是:

<input type="text" name="contactInfo.Address" id="contactInfo_Address" />

instead of:

代替:

@Html.TextBoxFor(m=> m.Address)

that results in:

结果是:

<input type="text" name="Address" id="Address" />

2. Pass the entire model:

2.通过整个模型:

If you pass the entire model the generated name would be correct, so you can use the helper:

如果您传递整个模型,生成的名称将是正确的,因此您可以使用帮助程序:

@Html.TextBoxFor(m=> m.contactInfo.Address)

that results in:

结果是:

<input type="text" name="contactInfo.Address" id="contactInfo_Address" />

回答by Balaji Selvarajan

Partial View page acts like User Control

部分视图页面的作用类似于用户控制

  1. Here _login.cshtml is partial view
  2. If you use partial view then underscore followed by name:

    <div style = "margin:3%;">@Html.Partial("~/Views/Shared/_login.cshtml",Model.login)
    </div>

  3. Model.login is a object. So in this object you can assign value whatever page.

  4. Partial will show based on object value.

  1. 这里_login.cshtml是局部视图
  2. 如果您使用局部视图,则下划线后跟名称:

    <div style = "margin:3%;">@Html.Partial("~/Views/Shared/_login.cshtml",Model.login)
    </div>

  3. Model.login 是一个对象。所以在这个对象中你可以为任何页面赋值。

  4. 部分将根据对象值显示。