PostAction中的ViewModel属性为Null [英] ViewModel Property Null in Post Action

查看:67
本文介绍了PostAction中的ViewModel属性为Null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在已解决.下面结合Ish的建​​议,再在视图中向@HiddenFor添加调用,即可解决此问题.

This is now fixed. A combination of Ish's suggestion below plus adding calls to @HiddenFor in the view resolved the problem.

我有一个ASP.NET MVC 5 Web应用程序,用户可以在其中将缺陷标记为已解决.我想显示一个潜在相关缺陷的列表,并带有复选框,用户可以选中这些复选框以表明是,这是相同的缺陷,还应标记为已解决.

I have an ASP.NET MVC 5 web application where users can mark a defect as resolved. I want to display a list of potentially related defects, with check-boxes that users can tick to indicate that yes, this is the same defect, and should also be marked as resolved.

因此,我有一个视图模型,该视图模型的属性是一个集合,该模型的每个成员都包含缺陷对象属性和布尔值IsSameDefect属性.所有这些在GET操作方法和视图中都可以正常工作.我可以显示相关缺陷并在方框中打勾.

So I have a View Model with a property that is a collection, each member of which contains a defect object property and Boolean IsSameDefect property. This all works fine in the GET action method and in the view. I can display the related defects and tick the boxes.

当我要更新数据时,POST操作中会出现问题.此时,属性(潜在相关缺陷的集合)为空.我很难弄清楚如何将这些数据传回控制器?

The problem arises in the POST action when I want to update the data. At this point the property (the collection of potentially related defects) is null. I'm having a hard time trying to figure out how to pass this data back to the controller?

根据要求提供代码...

Code as requested ...

// GET: /DefectResolution/Create
public ActionResult Create(int ciid)
{
    int companyId = User.CompanyID();
    DefectResolutionCreateViewModel drcvm = new DefectResolutionCreateViewModel(ciid, companyId);
    return View(drcvm);
}

// POST: /DefectResolution/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(DefectResolutionCreateViewModel drcvm)
{
    DefectResolutions currentResolution = drcvm.DefectResolution;
    currentResolution.CreatedOn = System.DateTime.Now;
    currentResolution.UserID = User.UserID();

    if (ModelState.IsValid)
    {
        unitOfWork.DefectResolutionRepository.Insert(currentResolution);

        if (currentResolution.ResolutionStatusID == 2)
        {
            //code breaks here as drcvm.RelatedUnresolvedDefects is null
            foreach (var relatedDefect in drcvm.RelatedUnresolvedDefects)
            {
                if (relatedDefect.IsSameDefect)
                {
                    DefectResolutions relatedResolution = new DefectResolutions();
                    relatedResolution.ChecklistID = relatedDefect.RelatedChecklist.ChecklistID;
                    relatedResolution.CreatedOn = System.DateTime.Now;
                    relatedResolution.ResolutionNote = currentResolution.ResolutionNote;
                    relatedResolution.ResolutionStatusID = currentResolution.ResolutionStatusID;
                    relatedResolution.UserID = User.UserID();
                }
            }
        }

        unitOfWork.Save();
        return RedirectToAction("Index", new { ciid = currentResolution.ChecklistID });
    }
    return View(drcvm);
}

在视图中...

@model Blah.ViewModels.DefectResolution.DefectResolutionCreateViewModel
@{
    ViewBag.Title = "Create Defect Resolution";
    var relatedDefects = Model.RelatedUnresolvedDefects;
}

...以及稍后在视图中...

... and later in the view ...

 @for (int i = 0; i < relatedDefects.Count(); i++ )
{
    <tr>
        <td>
            @Html.EditorFor(x => relatedDefects[i].IsSameDefect)
        </td>
    </tr>
}                       

我遵循以下Ish的建​​议,并修改了代码以直接引用Model.RelatedUnresolvedDefects,而不是像以前那样使用变量.这确实使我更进一步.视图模型的RelatedUnresolvedDefects属性不再为null.但是,只有RelatedUnresolvedDefects.IsSameDefect具有值. RelatedUnresolvedDefects.RelatedChecklist为null.这是控制器代码,再次显示了它现在在哪里中断了...

I followed Ish's suggestion below, and modified the code to refer to Model.RelatedUnresolvedDefects directly instead of using a variable as I had been doing. This does get me a bit further. The view model's RelatedUnresolvedDefects property is no longer null. But only RelatedUnresolvedDefects.IsSameDefect has a value. RelatedUnresolvedDefects.RelatedChecklist is null. Here's the controller code again showing where it now breaks ...

// POST: /DefectResolution/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(DefectResolutionCreateViewModel drcvm)
{
    DefectResolutions currentResolution = drcvm.DefectResolution;
    currentResolution.CreatedOn = System.DateTime.Now;
    currentResolution.UserID = User.UserID();

    if (ModelState.IsValid)
    {
        unitOfWork.DefectResolutionRepository.Insert(currentResolution);

        if (currentResolution.ResolutionStatusID == 2)
        {
            //prior to change, code used to break here
            foreach (var relatedDefect in drcvm.RelatedUnresolvedDefects)
            {
                if (relatedDefect.IsSameDefect)
                {
                    DefectResolutions relatedResolution = new DefectResolutions();

                    //code now breaks here because relatedDefect.RelatedChecklist is null
                    relatedResolution.ChecklistID = relatedDefect.RelatedChecklist.ChecklistID;
                    relatedResolution.CreatedOn = System.DateTime.Now;
                    relatedResolution.ResolutionNote = currentResolution.ResolutionNote;
                    relatedResolution.ResolutionStatusID = currentResolution.ResolutionStatusID;
                    relatedResolution.UserID = User.UserID();
                }
            }
        }

        unitOfWork.Save();
        return RedirectToAction("Index", new { ciid = currentResolution.ChecklistID });
    }
    return View(drcvm);
}

推荐答案

在不了解您的代码的情况下.建议您在视图(.cshtml)中呈现缺陷时使用for循环而不是foreach.

Without knowing your code.I suggest you to use for loop instead of foreach while rendering the defects in View (.cshtml).

根据您的代码编辑答案.
在视图创建问题中的以下声明

Editing Answer based on your code.
Following statement in the view creating problem

 var relatedDefects = Model.RelatedUnresolvedDefects;

您应该直接在循环中的Model.RelatedUnresolvedDefects属性上进行迭代.

You should directly iterate over the Model.RelatedUnresolvedDefects property in the loop.

             @for (int i = 0; i < Model.RelatedUnresolvedDefects.Count(); i++ )
                        {
                            <tr>
                                <td>
                                    @Html.EditorFor(x => Model.RelatedUnresolvedDefects[i].IsSameDefect)
                                </td>
                            </tr>
                        }

这篇关于PostAction中的ViewModel属性为Null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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