【问题标题】:Insert view with collection property插入带有集合属性的视图
【发布时间】:2014-01-09 00:33:38
【问题描述】:

我有一个 Person 类和一个 Contact 类。 一个人可以有很多联系人。

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")
}

我想要完成的是,用户可以在最终点击创建之前向 Person 添加多个联系人。

我有两个问题:

1- 这是最好的方法吗?难道没有像使用 javascript 或 jquery 这样更简单/更短的方法吗?

2- 当我第一次单击 AddContact 时效果很好,第二次 Person.Contacts 为空,而且我无法清除 AddContact 文本框。

我已经在整个网络和 Stackoverflow 上进行了搜索,但没有找到任何答案,有一个未回答的问题 here

PS:我是 MVC 新手,来自 ASP.NET Webforms。

【问题讨论】:

  • 我已经编辑了问题以提供代码并通过删除实体框架来简化它,只是一个具有集合属性的普通类。

标签: c# asp.net-mvc-5 asp.net-mvc-views


【解决方案1】:

以我个人的观点,我认为更好的方法是在客户端处理联系人“添加”,如果您没有在服务器端使用联系人数据做任何“有用”的事情(比如保存到数据库或其他东西)。

通过使用 jQuery 的几行 JavaScript,您可以为每个新联系人动态添加新的文本输入,当用户完成后,他只需点击提交,只需一次调用即可将所有信息发送到服务器。

这种方法会更好有两个原因,首先不会刷新页面以分散用户的注意力,其次您将避免用户多次调用服务器,这可能意味着根据您的流量节省 100 或 1000 次调用。

编辑:

This is a very simplified example 可能是什么,当然你应该添加更多的功能和样式。

您的视图模型应该看起来像这样才能使其工作:

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

这是一个常见的问题,所以肯定有很多其他的解决方案,看看this approach 也有点“旧”,但概念仍然适用。

【讨论】:

  • 很好,但我不知道如何使用 JS/JQ 将项目添加到网格/集合中,以便能够在 HTTPPost 中读回它,如果您可以为我提供示例或链接不胜感激,谢谢。
  • 当然,我会把它添加到我的答案中
  • 谢谢马科斯,很抱歉回复晚了,我目前无法测试,我会在可以的时候通知你。再次感谢!
  • 你有没有机会知道如何使用 webgrid 做到这一点?还有一个问题,无论我是使用 webgrid 还是手动遍历 Contacts 集合并放置控件,在第二次回发时集合又是空的。
猜你喜欢
  • 2015-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多