【问题标题】:How dynamicly add new partial form and how get data from them in ASP MVC?如何在 ASP MVC 中动态添加新的部分表单以及如何从中获取数据?
【发布时间】:2015-11-06 22:21:21
【问题描述】:

我有一个关于 ASP MVC 5 的项目。我有一个模型“文章”。这个模型有Author的HashSet和ICollection。作者 - 第二个模型:

 public partial class Article
{
    public Article()
    {
        Authors = new HashSet<Author>();
    }

    [DisplayName("Авторы")]
    public virtual ICollection<Author> Authors { get; set; }

我需要添加创建文章的页面,您可以在该页面上增加作者数量(使用AJAX),以及每个作者注册字段。我决定使用作者模型的部分视图,没有“创建”按钮(创建按钮仅用于创建文章的视图)。我需要无限添加新的局部视图,并在填充它们之后 - 从它们中获取所有数据。怎么弄?我是 MVC 的新手,无法想象它是如何工作的。

http://i.stack.imgur.com/0RHD0.png - 外观说明

【问题讨论】:

标签: asp.net ajax asp.net-mvc asp.net-mvc-4 partial


【解决方案1】:

这并不难。您的部分视图将作为集合发布。

假设您的局部视图有 2 个值,FirstName 和 LastName。应该是这样的:

@{
    Guid index = Guid.NewGuid();
}
<input type="hidden" name="People.index" value="@index" />
<input type="text" name="People[@index].FirstName" value="" />
<input type="text" name="People[@index].LastName" value="" />

最终的输出是:

<input type="hidden" name="People.index" value="B756DAD8-5D5D-449E-A4B4-E61F75C1562C" />
<input type="text" name="People[B756DAD8-5D5D-449E-A4B4-E61F75C1562C].FirstName" value="" />
<input type="text" name="People[B756DAD8-5D5D-449E-A4B4-E61F75C1562C].LastName" value="" />

<input type="hidden" name="People.index" value="B78B7BBC-EB0E-41CB-BE18-C1E3F7526F32" />
<input type="text" name="People[B78B7BBC-EB0E-41CB-BE18-C1E3F7526F32].FirstName" value="" />
<input type="text" name="People[B78B7BBC-EB0E-41CB-BE18-C1E3F7526F32].LastName" value="" />

您的模型必须有一个集合 People 对象。

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
} 

public class Article
{
    //other properties...

    public ICollection<Person> People { get; set; }
}

你的控制器:

public ActionResult YourAction (Article model)
{
    //...
}

未经测试的代码,但应该可以正常工作。

【讨论】:

  • 对不起,当我说 YourViewModel 时,我的意思是 Article 模型。我编辑了答案
  • 是的,我理解你。但是我把 [index] 放在哪里,如果我有这个: Html.LabelFor(model => model.surname, htmlAttributes: new { class= "control-label col-md-2" })
    Html.EditorFor(model => model.surname, new { htmlAttributes = new { class= "form-control" } }) Html.ValidationMessageFor(model => model.surname, "", new { class= " text-danger" })
    以及如何在控制器中获取作者?我不知道在 cshtml 中生成的 GUID
  • 您将索引放在所有输入之前。您必须使用Html.Editor("Authors[" + index + "].surname", model.surname, new { htmlAttributes = new { class = "form-control" } }) 而不是Html.EditorFor(model =&gt; model.surname, new { htmlAttributes = new { class = "form-control" } })。只要按照答案,一切都会好起来的
  • 这也应该有效@Html.TextBoxFor(model =&gt; model.surname, new { Name = "Author[index].surname", @class= "form-control"})
【解决方案2】:

是否需要使用局部变量?编写一个小脚本来克隆第一作者封闭元素并更改所涉及的元素的名称以创建新作者不是更容易吗?

<div id="enclosingDiv" data-count="x">
  <div class="someClass"  data-index='x1' >
    Author1 name Aurthor1 Textboxname="CollectionList[Index].Property"...
  </div>

现在在创建新的 Authouther 时,您可以创建:

<script>
  function createNewAuthor()
  {
       //clone first author
       var count = $('encolsingDiv').attr('data-count');
      //var count = $('encolsingDiv').children().length;
      var author = $('enclosingDiv').first().clone();
      //change name and id,etc using data-count
      author.find('*[name$='value'])attr('name','ListCollection[count  + 1]");
      author.find('*[name$='value'])attr('id',....);
      author.attr('data-index',count +1)
      $('enclosingDiv').append(author);
      $('enclosingDiv').attr('data-count',count + 1 to it);//makes life easier

  }

  function deleteAuthor(authourIndex)
  {
        //assumes that an author can be removed
        $('div[data-index="'+authorIndex+'"]").remove();
        $('enclosingDiv').children.each(function()
       {
           //if delete functionality exists, change the names of the element                       indices using the count variable
           change all indices of element properties concerned
           $(this).find('*[name$='value']).attr('name','ListCollection['+count+'].sumproperty");

           count++;
       });
  }
</script>

所以你可以将它用于创建和删除方法,你不需要部分。 代码可能需要一些工作,因为我展示的是概念

【讨论】:

  • Author1 name Aurthor1 Textboxname="CollectionList[Index].Property"... - 它是如何工作的?是 C# 代码吗?
  • 这只是一个插图,替换为剃须刀@Html.TextboxFor(x=&gt;x.Model.CollectionList[i].PropertyName)
  • 但是如果我有 ICollection? ICollection 没有索引器(
  • 您是否有特定原因希望它成为 ICollection 而不是另一个基于索引的集合?
猜你喜欢
  • 2017-05-10
  • 2010-10-14
  • 1970-01-01
  • 2016-09-04
  • 1970-01-01
  • 1970-01-01
  • 2017-11-30
  • 2023-04-03
  • 1970-01-01
相关资源
最近更新 更多