【发布时间】:2017-01-15 17:32:46
【问题描述】:
我正在 ASP.NET MVC 中开发一个 Web 应用程序(美食博客),我希望允许用户在其中输入食谱。食谱当然包含很多成分。 我有以下课程:
public class Recipe
{
public string Name { get; set; }
public int ID { get; set; }
public string Auhtor { get; set; }
public string Category { get; set; }
public string Text { get; set; }
public List<Ingredient> Ingredients { get; set; }
public byte[] Picture { get; set; }
}
public class Ingredient
{
public int ID { get; set; }
public double Amount { get; set; }
public string Unit { get; set; }
public string Name { get; set; }
}
在 html 中,我想创建一个页面,用户可以在其中输入包含大量成分的食谱。 到目前为止的创建视图如下所示:
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Recipe</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Category, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Category, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Category, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Text, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Text, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Text, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group" id="Ingredient">
@Html.LabelFor(model => model.Ingredients, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10" id="IngredientDiv">
<label>Ingredient</label>
</div>
</div>
<div class="AddEl">
<p><a>Tilføj endnu en ingredients</a></p>
</div>
<div class="RemoveEl">
<p><a>Fjern en ingredients</a></p>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Picture, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Picture, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Picture, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
<script type="text/javascript" src="/Scripts/jquery-1.10.2.js"></script>
<script>
//Ajax to navigate between days
$(document)
.ready(function () {
var counter = 1;
$('.AddEl').click(function () {
var el = $('#IngredientDiv').clone().attr('id', 'element_' + ++counter).appendTo('#Ingredient');
});
$('.RemoveEl').click(function () {
var el = $(this).parents('.elem');
if (counter > 1) {
el.remove();
counter--;
}
});
});
</script>
</div>
}
如何将成分链接到此视图? 如您所见,我已经编写了代码来动态添加和删除成分 div 的元素。
感谢您的帮助!
【问题讨论】:
-
我认为这会引导你走向正确的方向:google.pl/…
-
参考this answer了解动态添加收藏项的一些选项
标签: c# asp.net asp.net-mvc html razor