我的代码遇到了一些问题,希望有人可以帮忙。这是我正在使用的片段:
[Authorize]
public ActionResult EventResults(int id)
{
List<Event> CompetitionEvents = Event.getEventsByCompetitionId(id);
ViewBag.CompetitionEvents = CompetitionEvents;
List<Person> Competitors = Competition.getCompetitorsByCompetitionID(id);
ViewBag.Competitors = Competitors;
List<Results> Results = Competition.getCompetitorResultsPairings(CompetitionEvents, Competitors);
ViewBag.Results = Results;
ViewBag.OrganizerEmail = Competition.getCompetitionById(id).OrganizerEmail;
return View();
}
@model BINC.Models.Results
@using BINC.Models;
@{
var eventList = ViewBag.CompetitionEvents as List<Event>;
var competitorList = ViewBag.Competitors as List<Person>;
var resultList = ViewBag.Results as List<Results>;
}
<h2></h2>
<p>Results:</p>
@using (Html.BeginForm())
{
foreach (var evt in eventList)
{
<fieldset>
<legend>@evt.activity.Name</legend>
<p>Event Description: @evt.activity.Description</p>
@foreach (var competitor in competitorList)
{
foreach (var result in resultList)
{
if (result.EventID == evt.id && result.CompetitorEmail == competitor.Email)
{
<p>Competitor: @competitor.FirstName @competitor.LastName</p>
<p>Score: @result.Score</p>
if (ViewBag.OrganizerEmail.Equals(@User.Identity.Name))
{
@Html.LabelFor(model => model.Score, "New Score ");
@Html.TextBoxFor(model => model.Score, new { maxlength = 10, style = "width:125px" })
<input type="submit" name="submitButton" value="Update" />
}
}
}
}
</fieldset>
}
}
[HttpPost]
public ActionResult EventResults(Results res)
{
//stuff
}
我的问题只是在我的Results对象上设置了分数。 例如,当我将值“15”放入文本框并单击“更新”时,我将结果模型对象传递给httppost方法,该方法除了“得分”字段之外,其余都设置为空输入
我是否过于复杂?有更简单的方法吗?
我尝试添加
@Html.HiddenFor(model => model.EventID);
@Html.HiddenFor(model => model.CompetitorEmail);
但这似乎没有任何帮助。
答案 0 :(得分:0)
您有多个提交按钮,这可能是问题,这也不被视为良好做法
<input type="submit" name="submitButton" value="Update" />
在表单末尾只保留一个提交按钮
答案 1 :(得分:0)
基本上 - 确保你将模型传递给view--并使用Html Helpers(即TextBoxFor()和HiddenFor)
我不认为这是提交按钮的问题 - 但可能有帮助的一件事是将模型实际传递给视图。您正在使用ViewBag传递数据。将模型传递给View,您的Html Helpers应该生成正确的表单名称,以便模型绑定起作用。