MVC4怎么行项目动态添加到EditorFor场? [英] MVC4 how to dynamically add line item to EditorFor field?
问题描述
我有一个视图模型巫婆包含重复的项目。我将它们放置在通过EditorFor()方法,我的看法。
I have a view model witch contains iterative items. I place them in my view via the EditorFor() method.
查看:
@model Models.MyModel
@using (Html.BeginForm(@Model.Action, @Model.Controller))
{
<div class="section" id="Terms">
@Html.EditorFor(m => m.Terms)
</div>
<input type="submit" value="Save" />
}
型号:
public class MyModel
{
public IEnumerable<Term> Terms { get; set; }
}
EditorTemplates \\ Term.cshtml:
@model Models.Term
@if (Model != null)
{
<fieldset>
<legend>Term</legend>
@Html.HiddenFor(model => model.TermID)
<div class="editor-label">
@Html.LabelFor(model => model.Identifier)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Identifier)
@Html.ValidationMessageFor(model => model.Identifier)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Description)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Description)
@Html.ValidationMessageFor(model => model.Description)
</div>
</fieldset>
}
我希望能够动态地从列表视图中的添加/删除的项目,像knockout.js这样的例子,但我怎么preserve自动ID的MVC创建??
I want to be able to dynamically add / remove items from the list in the view, like this example on knockout.js, but how do I preserve the auto-id's MVC creates??:
<一个href=\"http://knockoutjs.com/examples/cartEditor.html\">http://knockoutjs.com/examples/cartEditor.html
下面是本我的要求:
- 新增条款
- 删除条款
- 验证添加新条款的意见
我读过上的其他问题,我还没有找到这个真正明确的答案。是knockout.js接受的方式做到这一点?是否有与敲除和MVC这样做的任何的例子?
I've read other questions on SO and I haven't found a real definitive answer on this. Is knockout.js the accepted way to do this? Are there any examples of doing this with Knockout AND MVC?
谢谢!
推荐答案
您想淘汰赛MVC http://knockoutmvc.com/ CartEditor
您不必使用淘汰赛对于这一点,你真正需要的是JavaScript和验证,并创建/删除其映射到对事物的MVC侧宁静的控制器操作的动作。你如何去实现那是你的。淘汰赛很容易,但。
You don't have to use knockout for this, what you really need is javascript with validations and create/delete actions which map onto restful controller actions on the MVC side of things. How you go about implementing that is up to you. Knockout makes it easy though.
这篇关于MVC4怎么行项目动态添加到EditorFor场?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!