简体   繁体   English

ASP.NET MVC 5-通过单击“添加行”按钮可以处理IEnumerable的任意数量的元素的“创建”视图?

[英]ASP.NET MVC 5 - “Create” view that can handle any number of elements for an IEnumerable based on clicking an “Add row” button?

I am writing a recipe manager for my wife in C#/.NET MVC 5. I'm getting to the page for creating a recipe, and I'm a little stumped. 我正在用C#/。NET MVC 5为我的妻子编写一个食谱管理器。我要进入创建食谱的页面,但有些困惑。 A recipe consists of a Name and a list of ingredients. 食谱由名称和成分列表组成。

When I create a view, I have my form: 创建视图时,具有以下表单:

@using(Html.BeginForm()){
    //Form elements
    @Html.DisplayNameFor(x => Model.Name)
    //button for adding a new ingredient to the recipe 
    <input type="submit" text="Submit New Recipe!" />
}

When the button for adding an ingredient is clicked, it should render a block of html inside the form just above the button itself, that way the user can add any number of ingredients and then submit the recipe back when the form is posted back to the controller. 单击添加成分的按钮时,它应该在按钮本身上方的表单内部呈现html块,这样用户可以添加任意数量的成分,然后在表单回发到表单时提交配方。控制器。

For this functionality, should I make the button call a controller that sends back a partial view or something? 对于此功能,我是否应该让按钮调用一个发送回部分视图或其他内容的控制器? I'm not sure how to accomplish this outside of JavaScript, but I'm wanting to use a .NET MVC solution if I can. 我不确定如何在JavaScript之外完成此操作,但是如果可以的话,我想使用.NET MVC解决方案。

I try to minimize my reliance on javascript whenever I can, however I agree with @br4d that knockout is your best option here. 我会尽可能地减少对javascript的依赖,但是我同意@ br4d的建议,敲除是这里的最佳选择。 If you want to avoid it at all cost, it will be more complex, slower and not as user friendly but here is how I would do it. 如果您想不惜一切代价避免它,它将变得更复杂,更慢并且不那么用户友好,但是这就是我要做的。

Enclose the form in a div. 将表格括在div中。 Have a place holder div inside the form to hold your ingredients list. 在表格内有一个占位符div来保存您的配料表。 Make the "Add new ingredient" call into a controller that will return a partial view with the required fields. 在控制器中进行“添加新成分”调用,该控制器将返回带有必填字段的部分视图。 In the target attribute indicate the place holder div as the update target and append the response to the html of the place holder div by specifying InsertionMode.InsertAfter. 在target属性中,将占位符div指示为更新目标,并通过指定InsertionMode.InsertAfter将响应附加到占位符div的html。

<div id="parentDiv">
    @Html.BeginForm........
        @Ajax.ActionLink("Add New Ingredient","ActionName","ControllerName",routeValues,
                     new AjaxOptions
                    {
                        UpdateTargetId = "ChildDiv",
                        InsertionMode = InsertionMode.InsertAfter
                    }
        <div id=ChildDiv>
        </div>
</div>

This code is by no means comprehensive or production ready (I prefer to have a way of handling failed ajax calls and you might want to block off interactions until the call comes back just to mention two of the enhancements). 这段代码绝不是全面的或准备就绪的(我更喜欢采用一种处理失败的Ajax调用的方式,您可能希望阻止交互,直到调用回来仅提及其中的两个增强功能为止)。 Once again KnockOut would be the preferred way to do this. 再一次,KnockOut将是执行此操作的首选方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM