我的代碼存在一些問題,希望有人能幫我一把。以下是我正在處理的代碼段:[HttpPost]中的模型值爲空
[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
}
我的問題不過是在我的結果對象上設置了分數。 例如,當我將值'15'放入文本框並單擊'Update'時,我將Result模型對象傳遞給httppost方法,它將除'score'字段之外的所有內容設置爲null剛剛進入。
我是否過分複雜呢?有更容易的方法嗎?
我嘗試添加
@Html.HiddenFor(model => model.EventID);
@Html.HiddenFor(model => model.CompetitorEmail);
,但似乎並沒有幫助任何。