【发布时间】:2017-08-02 22:34:37
【问题描述】:
我正在做一个课堂实验,其中包括设计一个小型众筹网站。当用户创建一个项目时,系统会询问他想要为他的项目获得多少层奖励。如果此金额大于 0,他将被重定向到奖励/创建页面。 “创建”视图中有一个表单,该表单是根据用户想要的层数生成的。
问题是我不知道如何使用一个提交按钮将“for”循环的每个实例链接到特定的“RewardViewModel”对象,以及如何将所有这些对象传递给控制器。
这是控制器“Get”方法(我知道我可能不得不更改绑定到视图的模型):
public ActionResult Create(int projectid, int amountOfTiers )
{
ViewBag.AmountOfTiers = amountOfTiers;
ViewBag.ProjectTitle = (from p in context.Projects where p.ProjectId == projectid select p).FirstOrDefault().Title;
RewardViewModel model = new RewardViewModel { ProjectId = projectid };
return View(model);
}
这是视图:
@model CrowdFundMe.ViewModels.RewardViewModel
@{
ViewBag.Title = "Create";
}
<body>
@section Login{
@Html.Action("_Login", "User")
}
<h2>Create</h2>
<p>You are adding rewards for the project @ViewBag.ProjectTitle</p>
@using (Html.BeginForm("Create", "Reward", FormMethod.Post))
{
var u = ViewBag.AmountOfTiers;
for (int i = 0; i < u; i++)
{
var tier = i+1;
@Html.HiddenFor(model => model.ProjectId)
@Html.LabelFor(model => model.Tier, "Tier")<br />
@Html.TextBoxFor(model => model.Tier, new {@Value = tier, @readonly="readonly"})
<br />
<br />
@Html.LabelFor(model => model.Title, "Title")<br />
@Html.TextBoxFor(model => model.Title)
@Html.ValidationMessageFor(model => model.Title)
<br />
<br />
@Html.LabelFor(model => model.AmountOfItems, "Quantity available (leave blank if unlimited)")<br />
@Html.TextBoxFor(model => model.AmountOfItems)
@Html.ValidationMessageFor(model => model.AmountOfItems)
<br />
<br />
@Html.LabelFor(model => model.Description, "Description")<br />
@Html.EditorFor(model => model.Description)
@Html.ValidationMessageFor(model => model.Description)
<br />
<br />
}
<input type="submit" value="Create" />
}
【问题讨论】:
标签: asp.net-mvc