ASP.NET MVC 3 如何在创建视图上为具有 ICollection 属性的模型提供多字段创建功能

发布于 2024-10-10 07:47:34 字数 1226 浏览 1 评论 0原文

注意:我使用的是MVC3+Razor、EF4、CF-CTP5

  1. 如何让视图能够在客户端上为每个组织动态添加多个地址类,并强绑定到发布时的模型?

  2. 如果 (ModelState.IsValid == false),如何让视图解析模型中的值,这样如果您输入 3 个地址并发布无效模型,它会重新填充数字地址及其适当的值?

这是我的模型:

public class Organization
{
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Address> Addresses { get; set; }
    public virtual ICollection<PhoneNumber> PhoneNumbers { get; set; }
    ...
}

public class Address
{
    public int Id { get; set; }
    public string Line1 { get; set; }
    public string Line2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string PostalCode { get; set; }
    public string Country { get; set; }
    public int Type { get; set; }
}

我试图弄清楚如何让组织的创建操作 (/Organization/Create) 像这样处理创建(这样地址和电话号码是提交模型的一部分):

[HttpPost]
public ActionResult Create(Organization organization)
{
    if (ModelState.IsValid)
    {
        _db.Organizations.Add(organization);
        _db.SaveChanges();
        return RedirectToAction("Details", organization.Id);
    }

    return View(organization);

}

Note: I'm using MVC3+Razor, EF4, CF-CTP5

  1. How can you allow the view to have the ability to add multiple Address classes per Organization dynamically on the client, and bound strongly to the model on post?

  2. How can you have the view parse values in the model if the (ModelState.IsValid == false) such that if you enter 3 addresses and post an invalid model, it re-populates the number addresses and with their appropriate values?

Here are my models:

public class Organization
{
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Address> Addresses { get; set; }
    public virtual ICollection<PhoneNumber> PhoneNumbers { get; set; }
    ...
}

public class Address
{
    public int Id { get; set; }
    public string Line1 { get; set; }
    public string Line2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string PostalCode { get; set; }
    public string Country { get; set; }
    public int Type { get; set; }
}

I'm trying to figure out how you can have the Create action for Organization (/Organization/Create) handle the create like thus (such that addresses and phone numbers are part of the submitted model):

[HttpPost]
public ActionResult Create(Organization organization)
{
    if (ModelState.IsValid)
    {
        _db.Organizations.Add(organization);
        _db.SaveChanges();
        return RedirectToAction("Details", organization.Id);
    }

    return View(organization);

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

那些过往 2024-10-17 07:47:34

你的问题相当广泛:)
这只是实现您的要求的方式之一,我相信有比我更好的方式。

我将从你的第二个问题开始:

如何获得视图解析值
在模型中如果
(ModelState.IsValid == false) 这样
如果您输入 3 个地址并发布
无效模型,它会重新填充
号码地址及其
合适的值?

如果我正确理解你的要求,对我来说这看起来很简单。答案很简单:对视图进行编码以呈现模型类内容,并将无效模型返回给客户端,就像您在 Create 中所做的那样行动。

如果您的表单(及其字段)已使用 ValidationSummary/ValidationMessage html 帮助程序进行修饰,您还将看到验证消息。

  1. 如何让视图能够添加多个地址
    每个组织的类别动态
    在客户端上,并强烈绑定到
    帖子上的模特?

您可以有一个显示组织属性的主视图,然后有另一个显示相关地址的视图。您可以在此处放置一个超链接或一个按钮,打开一个对话框以添加新地址对象,然后在完成后刷新地址列表。同样,您可以将编辑和删除按钮作为列表上的图标。

地址列表是完全在客户端处理的一个标记,要正确绑定到服务器端模型类,应该为其输入属性遵守一些简单的命名规则。

要使默认模型绑定器类正确绑定您的表单,请在您的 Organization 类中使用以下代码片段

@using (Html.BeginForm()) {
    @Html.HiddenFor(o => o.Id)
    @Html.ValidationSummary( true )
    <fieldset>
        <legend>My Organization</legend>
        <div class="editor-label">@Html.LabelFor( model => model.Name )</div>
        <div class="editor-field">
            @Html.EditorFor( model => model.Name )
            @Html.ValidationMessageFor( model => model.Name )
        </div>
        <br />
        <div id="container">
            <div>Address List</div>
            @foreach (Address a in Model.Addresses ) {
                Html.EditorFor(a);
            }
        </div>
        <div style="text-align:right;margin-top:14px;">
            <input type="submit" id="btnSubmit" value="Save" />
        </div>
    </fieldset>
}

要自动绑定,表单的生成代码应如下所示,

<form action="..." id="..." method="post">
    <input type="hidden" name="Id" value="2">
    <input type="hidden" name="Name" value="Acme Corporation">

    <!-- markup for each address -->
    <input type="hidden" name="Addresses[0].Id" value="1">
    <input type="hidden" name="Addresses[0].Line1" value="Line 1">
    <input type="hidden" name="Addresses[0].Line2" value="Line 2">
    ... and so on...
</form>

其属性名为 Addresses [索引].PropertyName
如果您在客户端添加新地址,那么这并不重要:只要您的代码遵守此规则,您就可以让默认的 Model Binder 为您完成这项工作。

希望这有帮助

Your question is quite vaste :)
This is just one of the way your requirement can be achieved and I am sure there are better than mine.

I am going to start from your second question:

How can you have the view parse values
in the model if the
(ModelState.IsValid == false) such
that if you enter 3 addresses and post
an invalid model, it re-populates the
number addresses and with their
appropriate values?

If I correctly understand your request it looks very simple to me. The answer is simply code your view to render a Model class content and return the invalid model to the client exactly as you are doing in your Create action.

If your form (and its fields) have been decorated with the ValidationSummary/ValidationMessage html helpers, you are going to see also validation messages.

  1. How can you allow the view to have the ability to add multiple Address
    classes per Organization dynamically
    on the client, and bound strongly to
    the model on post?

You can have a main view showing Organization attributes and then have another view showing related addresses. Here you can place a hyperlink or a button that open a dialog for adding a new address object and then refresh the address list when done. At the same way you can have edit and delete buttons as icons on the list.

The address list is a piece of markup completely handled at client side that, to be correctly binded to the server side Model class should adhere to some simple naming rules for it's input attributes.

To make the Default Model Binder class bind correctly your form use the following snippet for your Organization class

@using (Html.BeginForm()) {
    @Html.HiddenFor(o => o.Id)
    @Html.ValidationSummary( true )
    <fieldset>
        <legend>My Organization</legend>
        <div class="editor-label">@Html.LabelFor( model => model.Name )</div>
        <div class="editor-field">
            @Html.EditorFor( model => model.Name )
            @Html.ValidationMessageFor( model => model.Name )
        </div>
        <br />
        <div id="container">
            <div>Address List</div>
            @foreach (Address a in Model.Addresses ) {
                Html.EditorFor(a);
            }
        </div>
        <div style="text-align:right;margin-top:14px;">
            <input type="submit" id="btnSubmit" value="Save" />
        </div>
    </fieldset>
}

To be automatically bindable the resultant code for the form should look as the following

<form action="..." id="..." method="post">
    <input type="hidden" name="Id" value="2">
    <input type="hidden" name="Name" value="Acme Corporation">

    <!-- markup for each address -->
    <input type="hidden" name="Addresses[0].Id" value="1">
    <input type="hidden" name="Addresses[0].Line1" value="Line 1">
    <input type="hidden" name="Addresses[0].Line2" value="Line 2">
    ... and so on...
</form>

having it's properties named as Addresses[index].PropertyName.
If you add new addresses on the client it does'nt matter so much: as long as your code respect this rule you can have the default Model Binder do the job for you.

Hope this helps

您的好友蓝忘机已上羡 2024-10-17 07:47:34

我不确定我是否正确理解你的问题,但关于问题 1,我认为你正在寻找 ViewModel。也许像这样..

OrganizationViewModel.cs

public class OrganizationViewModel
{
  public OrganizationViewModel(Organization org, IList<Address> addresses)
     {
       this.Organization = org;
       this.Addresses = addresses
     }
     public Organization Organization {get;set;}
     public IList<Address> Addresses {get;set;}
}

OrganizationController.cs

public class OrganizationController : Controller
{
  private readonly IOrganizationService _organizationService: //or whatever method you use
  public OrganizationController(IOrganizationService orgService)
     {
       this._organizationService = orgService;
     }

   public ActionResult Item(int id)
     {
       var org = _organizationService.GetOrganizationById(id);
       var addresses = _organizationService.GetOrgAddressesByOrgId(id);
       return View(new OrganizationViewModel(program, addresses));
     }
}

Item.cshtml

@model OrganizationViewModel
<h1>@Model.Organization.Name</h1>
<ul>
@foreach(var a in Model.Addresses)
{
<li>@a.Line1</li>
<li>@a.Line2</li>}
</ul>

在我尝试回答第 2 个问题之前,也许您应该指出我是否正确理解了问题 1。希望这会有所帮助。

I'm not sure if I understand your question correctly but with respect to question 1 I think you are looking for a ViewModel. Like this perhaps..

OrganizationViewModel.cs

public class OrganizationViewModel
{
  public OrganizationViewModel(Organization org, IList<Address> addresses)
     {
       this.Organization = org;
       this.Addresses = addresses
     }
     public Organization Organization {get;set;}
     public IList<Address> Addresses {get;set;}
}

OrganizationController.cs

public class OrganizationController : Controller
{
  private readonly IOrganizationService _organizationService: //or whatever method you use
  public OrganizationController(IOrganizationService orgService)
     {
       this._organizationService = orgService;
     }

   public ActionResult Item(int id)
     {
       var org = _organizationService.GetOrganizationById(id);
       var addresses = _organizationService.GetOrgAddressesByOrgId(id);
       return View(new OrganizationViewModel(program, addresses));
     }
}

Item.cshtml

@model OrganizationViewModel
<h1>@Model.Organization.Name</h1>
<ul>
@foreach(var a in Model.Addresses)
{
<li>@a.Line1</li>
<li>@a.Line2</li>}
</ul>

Before I try and answer number 2 maybe you should indicate whether I am correctly understanding question 1. Hope this helps.

洛阳烟雨空心柳 2024-10-17 07:47:34

我设法使用 LINQ to SQL 来做到这一点。现在我尝试使用实体框架,但它确实使一切变得更加复杂。所以我没有适合您的解决方案,但也许我的 L2S 解决方案可能有帮助?

使用从我的数据库生成的模型,我可以在我的视图中执行此操作:

@for (int i = 0; i < Model.Contact.EmailAddresses.Count; ++i)
{
    <li>
        @Html.TextBoxFor(x => x.Contact.EmailAddresses[i].EmailAddress)
        @Html.HiddenFor(x => x.Contact.EmailAddresses[i].EmailAddressID)
    </li>
} 

我有一个视图模型类:

class ContactViewModel
{
    Contact contact { get; set; }
}

这工作正常,在我的控制器操作中,我得到了我的联系人对象,它的 Contact.ContactEmailAddresses 列表填充就像我预期的那样。

但对于 EF,我无法再使用从数据库生成的 EmailAddresses 属性上的 [i]。我想出的最好的办法是:

@Html.TextBox("Contact.EmailAddresses[" + i + "].EmailAddress", Model.Contact.EmailAddresses.ElementAt(i).EmailAddress)

I managed to do this using LINQ to SQL. Now I'm trying to use Entity Framework instead, but it really makes everything more complicated. So I don't have a solution for you, but perhaps my L2S solution might help?

Using models generated from my database I could in my view do this:

@for (int i = 0; i < Model.Contact.EmailAddresses.Count; ++i)
{
    <li>
        @Html.TextBoxFor(x => x.Contact.EmailAddresses[i].EmailAddress)
        @Html.HiddenFor(x => x.Contact.EmailAddresses[i].EmailAddressID)
    </li>
} 

I had a view model class:

class ContactViewModel
{
    Contact contact { get; set; }
}

This worked fine and in my controller action I got my Contact object with it's Contact.ContactEmailAddresses list filled just like I expected.

But with EF, I cannot use the [i] on the EmailAddresses property generated from the database anymore. The best I have come up with is:

@Html.TextBox("Contact.EmailAddresses[" + i + "].EmailAddress", Model.Contact.EmailAddresses.ElementAt(i).EmailAddress)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文