插入观点与集合属性 [英] Insert view with collection property

查看:186
本文介绍了插入观点与集合属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Person类和联系类。
一个人可以有很多的接触。

I have a Person class and a Contact class. A person can have many contacts.

public class Person
{
    [Required]
    public string FirstName { get; set; }

    public string LastName { get; set; }

    public string Notes { get; set; }

    [Required]
    public List<Contact> Contacts { get; set; }


    public Person()
    {
        Contacts = new List<Contact>();
    }
}


public class Contact
{
    [Required]
    public string Title { get; set; }

    [Required]
    public string Value { get; set; }

    public Contact() { Title = ""; Value = ""; }

    public Contact(string title, string value)
    {
        Title = title;
        Value = value;
    }
}

控制器

public class PersonController : Controller
{

    public ActionResult Create()
    {
        return View(new PersonCreateModel());
    }


    // POST: /Person/Create
    [HttpPost]
    public ActionResult Create(string btnSubmit, PersonCreateModel personModel)
    {
        try
        {
            switch (btnSubmit)
            {
                case "AddContact":
                    personModel.Person.Contacts.Add(new Contact(personModel.NewContact_Title, personModel.NewContact_Value));
                    personModel.NewContact_Title = personModel.NewContact_Value = "";
                    return View(personModel);

                case "CreatePerson"://Add To Database
                    //blabla
                    break;
            }

            return RedirectToAction("Index");
        }
        catch
        {
            return View(personModel);
        }
    }
}

PersonCreateModel

public class PersonCreateModel
{

    public Person Person { get; set; }

    public string NewContact_Title { get; set; }
    public string NewContact_Value { get; set; }

    public PersonCreateModel()
    {
        Person = new Person();
    }
}

查看

@model MvcApplication1.Models.PersonCreateModel

@{
    ViewBag.Title = "Create";

    var contactsGrid = new WebGrid(Model.Person.Contacts); 
}

<h2>Create</h2>

@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Person</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Person.FirstName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Person.FirstName)
            @Html.ValidationMessageFor(model => model.Person.FirstName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Person.LastName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Person.LastName)
            @Html.ValidationMessageFor(model => model.Person.LastName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Person.Notes)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Person.Notes)
            @Html.ValidationMessageFor(model => model.Person.Notes)
        </div>

        <br />
        <div>
            <h4>Contacts:</h4>

            <table>
                <tr>
                    <td>Title: @Html.EditorFor(model => model.NewContact_Title)</td>
                    <td>Value: @Html.EditorFor(model => model.NewContact_Value)</td>
                    <td>
                        <input type="submit" name="btnSubmit" value="AddContact" /></td>
                </tr>
            </table>

            <div>
                @contactsGrid.GetHtml()
            </div>
        </div>

        <p>
            <input type="submit" name="btnSubmit" value="CreatePerson" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

我试图做到的是,用户终于可以单击创建前人添加多个联系人。

What I'm trying to accomplish is that the user can add multiple contacts to the Person before finally clicking Create.

我有两个问题:

1 - 这是最好的办法?是不是有这样喜欢使用JavaScript或jQuery的一个更简单/短的办法?

1- Is this the best approach?, isn't there an easier/shorter way of doing this like using javascript or jquery?

2 - 当我点击的addContact第一次它的工作Person.Contacts是空很大,第二次,也是我无法清除的addContact文本框。

2- when I click AddContact the first time it works great, second time the Person.Contacts is Empty, and also I can't clear the AddContact Textboxes.

我已经搜索所有网站上,并在这里#2,但我没有找到任何答案,有一个悬而未决的问题<一href=\"http://stackoverflow.com/questions/19637369/how-to-update-multiple-many-to-many-relationship-and-check-for-concurrency-excep?rq=1\">here

I've searched all over the web and here on Stackoverflow but I didn't find any answers, there is an unanswered question here

PS:我是新来的MVC,从ASP.NET Web表单来

PS: I'm new to MVC, coming from ASP.NET Webforms.

推荐答案

在我个人的观点,我认为更好的办法是处理,并称在客户端上的联系人,如果你没有做任何事情与联系人的数据(如保存到数据库,或者别的什么)服务器端的有用。

In my personal point of view, I think a better approach would be to handle the contacts "adding" in the client side, if you aren't doing anything "useful" in the server side with the contact's data (like saving to database or something else).

通过使用jQuery,你可以动态地添加新的文本输入,为每一个新的接触,而当用户完成,他可能只是点击提交,并在短短一个呼叫发送到服务器的所有信息的JavaScript小线。

With little lines of JavaScript using jQuery, you could dynamically add new text inputs for every new contact, and when the user is done, he could just hit submit and send all the information in just one call to the server.

这个方法是有两个原因,首先就没有页面清爽分散用户,二是你会避免用户多个服务器调用,这可能意味着数百个或1000个保存的通话根据您的流量。

This approach would be better for two reasons, first there would be no page refreshing to distract the user, and second you'll avoid multiple server calls by user, that could mean 100s or 1000s calls saved depending on your traffic.

编辑:

这是什么的会不会是一个很简单的例子,当然你应该增加更多的功能,和造型。

This is a very simplified example of what could it be, of course you should add more functionality, and styling.

您的视图模型应该是这个样子,为了这个工作:

Your view model should look something like this in order to this to work:

public class PersonCreateModel
{
    public Person Person { get; set; }
    public List<Contact> Contacts { get; set; }
}

这是一个常见的​​问题,所以必须有其他很多的解决方案,来看看的这种做法太的它是一种老,但概念仍然适用。

This is a common problem, so there must be a lot of other solutions, take a look at this approach too it's kind of "old" but the concepts still apply.

这篇关于插入观点与集合属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆