模型MVC3数据发布时设置为null [英] MVC3 Data in model set to null when posting

查看:100
本文介绍了模型MVC3数据发布时设置为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个观点,现在那里的模型是得到传递到局部视图和页面上的列表中显示的型号列表。局部视图具有,提交时,应发送的模型回控制器在那里它可以重定向模型到页面用于编辑其属性的形式。然而,该模型的数据成员之一被设置为null发布后,我想不通为什么。这里的code,我使用了影响这部分程序:

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:

示范

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

}

在主视图

<ul id="all_entries">
@foreach (var entry in Model)
{
    <div id="@String.Format("entry{0}", entry.ActivityID)">@Html.Partial("UserEntryRow", entry)</div>
}

在局部视图

@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>

在控制器方法它发布到

[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");
    }

在它的帖子,model.ActivityModel为空,即使在调试时它被正确设置视图中。任何想法,这可能是导致此?

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?

推荐答案

我觉得你有一些问题在这里讨论。

I think you've got a few issues to address here.


  1. ViewEntryModel.ActivityModel 被声明为类型的 Activityl ,所以绑定系统会尝试创建为那个类的一个实例。这条线将因此总是失败:

  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;


  • 看你如何使用活动类 - 为活动抽象?如果是这样,这就是为什么绑定系统是把它当作空。

  • Seeing how you're using the Activity classes - is Activity abstract? If so, that's why the binding system is leaving it as null.

    的部分没有发布任何的活动的属性的属性;尝试添加

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

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

    ...或者类似的东西;什么是适当的活动类。

    如果你想坚持使用 ActivityType / ActivityModel 属性,你是,你可以创建一个自定义<根据 ActivityType 值code> ValueProvider 以创建活动的对象。或者,你可以为每个活动类型创建专用 ViewEntryModel 类型,并将其发布到不同的动作。

    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.

    这篇关于模型MVC3数据发布时设置为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆