0

我有一个使用ViewModel. 在里面ViewModel我这样做(编辑显示完整ViewModel

namespace MyNameSpace.ViewModels
{
    public class MyViewModel
    {
        public ModelOne ModelOne { get; set; }
        public ModelTwo ModelTwo { get; set; }
    }
}

在我的主要观点上,我这样做(编辑:添加@Html.Hidden代码)

@using MyNameSpace.ViewModels
@using MyNameSpace.Models
@model MyViewModel

...
@using (Html.BeginFormAntiForgeryPost())
{ 
    @Html.Hidden("myData", new MvcSerializer()
        .Serialize(Model, SerializationMode.Signed))
....
<div>
    @{Html.RenderPartial("_MyCheckBox", 
        Model.ModelTwo, new ViewDataDictionary());}
</div>
}
....

我的部分观点是:

@using MyNameSpace.Models
@model ModelTwo
<div>
    <fieldset>
        <div class="editor-label">
            @Html.LabelFor(x => x.MyCheckBox)</div>
        <div class="editor-field">
            <select multiple="multiple" id="@Html.FieldIdFor(x =>
                x.MyCheckBox)" name="@Html.FieldNameFor(x =>
                x.MyCheckBox)">

                @foreach (MyEnum item in Enum.GetValues(typeof(MyEnum)))
                {
                    var selected = Model.MyCheckBox.Contains(item); //ERROR HERE
                    <option value="@((int)item)" @if (selected)
                        {<text>selected="selected"</text>}>
                        @T(item.ToString())
                    </option>
                }
            </select>
        </div>
    </fieldset>
</div>

我收到了Object reference not set to an instance ...错误,不知道如何纠正它。

最初,我<fieldset>在我的主视图中有这个并且得到了那个错误。我以为是因为这两个模型,所以我把它放在局部视图中。但只是发现我遇到了同样的问题。

我没有正确设置MyCheckBox我的部分视图var selected = Model.MyCheckBox.Contains(item);

有什么想法吗?

编辑(添加MyCheckBox代码)

这是MyCheckBox里面ModelOne.cs

[Mandatory(ErrorMessage = "Please select at least one option")]
[Display(Name = "Please select one ore more options")]
[MySelector]
public virtual int MyCheckBox { get; set; }

它在ModelTwo.cs里面:

private IList<MyEnum> _myEnum;
public IList<MyEnum> MyCheckBox
{
    get
    {
        if (_myEnum== null)
        {
            _myEnum= new List<MyEnum>();
            foreach (MyEnumitem in Enum.GetValues(typeof(MyEnum)))
            {
                if (((MyEnum)Record.MyCheckBox& item) == item)
                    _myEnum.Add(item);
            }
        }
        return _myEnum;
    }
    set
    {
        _myEnum= value;
        Record.MyCheckBox= 0;
        foreach (var item in value)
        {
            Record.MyCheckBox|= (int)item;
        }
    }
}

请注意,我使用的是 Orchard(因此是Record),而后者又使用 NHibernate。我不认为这是相关的,但我可能是错的。该MyCheckBox代码使用[Flags]属性enum并将值存储为int数据库(因此是代理)。下面是它的enum样子:

我的枚举.cs

[Flags]
public enum MyEnum
{
    [Display(Name="Name 1")]
    Enum1 = 1,
    [Display(Name="Name 2")]
    Enum2 = 2,
    [Display(Name="Name 3")]
    Enum3 = 4,
    [Display(Name="Name 4")]
    Enum4 = 8,
    [Display(Name="Name 5")]
    Enum5 = 16
}

我的控制器.cs

private MyViewModel myData;
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
    var serialized = Request.Form["myData"];
    if (serialized != null) //Form was posted containing serialized data
    {
    myData= (MyViewModel)new MvcSerializer().Deserialize
        (serialized, SerializationMode.Signed);
        TryUpdateModel(myData);
    }
    else
        myData= (MyViewModel)TempData["myData"] ?? new MyViewModel();
    TempData.Keep();
}
protected override void OnResultExecuted(ResultExecutedContext filterContext)
{
    if (filterContext.Result is RedirectToRouteResult)
        TempData["myData"] = myData;
}

然后在Action*MyController.cs 中的特定内容中:

public ActionResult Step5(string backButton, string nextButton)
{
    if (backButton != null)
        return RedirectToAction("Step4");
    else if ((nextButton != null) && ModelState.IsValid)
        return RedirectToAction("Confirm");
    else
        return View(myData);
    }
4

0 回答 0