MVC3 Data in model set to null when posting

左心房为你撑大大i 提交于 2019-12-12 02:40:10

问题


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.

  1. ViewEntryModel.ActivityModel is declared as being of type Activityl, 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;
    
  2. Seeing how you're using the Activity classes - is Activity abstract? If so, that's why the binding system is leaving it as null.

  3. The partial doesn't post any of the Activity property's properties; try adding

    @Html.TextBoxFor(m => m.Activity.Name)
    

    ...or something like that; whatever is appropriate for the Activity class.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!