问题
I have a view right now where the model is a list of models that get passed into a partial view and displayed as a list on the page. The partial view has a form that, when submitted, should send the Model back to the controller where it can redirect the model to a page for editing its properties. However, one of the data members of the model is set to null after posting and I can't figure out why. Here's the code I'm using that affects this part of the program:
The Model
public class ViewEntryModel
{
#region properties
public long ActivityID { get; set; }
public Activity ActivityModel { get; set; }
public string ActivityType { get; set; }
public DateTime Date { get; set; }
public string ActivityName { get; set; }
public string Answer { get; set; }
#endregion
}
In the main View
<ul id="all_entries">
@foreach (var entry in Model)
{
<div id="@String.Format("entry{0}", entry.ActivityID)">@Html.Partial("UserEntryRow", entry)</div>
}
In the Partial View
@model Web.Models.ViewEntryModel
<div class="entryInformation">
<div class="left">
<span>@Model.Date.Month/@Model.Date.Day/@Model.Date.Year</span>
</div>
<div class="right">
@using(Html.BeginForm("EditEntry", "UserEntry", Model))
{
<input type="submit" value="Edit" />
} <a>delete</a>
</div>
<div class="middle">
@if (Model.ActivityType.Equals("Exercise"))
{
<span>You completed the <strong>@Model.ActivityName</strong> exercise for <strong>@Model.Answer</strong>.</span>
}
else if (Model.ActivityType.Equals("Question"))
{
<span>You answered <strong>@Model.ActivityName</strong> with <strong>@Model.Answer</strong>.</span>
}
else if (Model.ActivityType.Equals("Nutrition"))
{
<span>You ate <strong>@Model.Answer</strong> servings of <strong>@Model.ActivityName</strong>.</span>
}
</div>
</div>
The Controller method it's posting to
[HttpPost]
[Authorize(Roles = "User")]
public ActionResult EditEntry(ViewEntryModel model)
{
if (model.ActivityType.Equals("Exercise"))
{
ExerciseActivity exerciseActivity = (ExerciseActivity)model.ActivityModel;
return RedirectToAction("LogPastExerciseActivity", "UserDashboard", exerciseActivity);
}
return RedirectToAction("Index");
}
When it posts, model.ActivityModel is null, even though when debugging it was correctly set inside the view. Any ideas as to what could be causing this?
回答1:
I think you've got a few issues to address here.
ViewEntryModel.ActivityModelis declared as being of typeActivityl, so the binding system will try and create it as an instance of that class. This line will therefore always fail:ExerciseActivity exerciseActivity = (ExerciseActivity)model.ActivityModel;Seeing how you're using the
Activityclasses - isActivityabstract? If so, that's why the binding system is leaving it as null.The partial doesn't post any of the
Activityproperty's properties; try adding@Html.TextBoxFor(m => m.Activity.Name)...or something like that; whatever is appropriate for the
Activityclass.
If you want to stick with using ActivityType / ActivityModel properties as you are, you could create a custom ValueProvider to create Activity objects based on the ActivityType values. Alternatively, you could create dedicated ViewEntryModel types for each Activity type, and post them to different actions.
来源:https://stackoverflow.com/questions/9069496/mvc3-data-in-model-set-to-null-when-posting