【问题标题】:Null Entry for NonNullable Type in ASP.Net MVC 5 projectASP.Net MVC 5 项目中 NonNullable 类型的 Null 条目
【发布时间】:2018-02-19 02:40:44
【问题描述】:

我在模型中有一个属性需要是不可为空的。如果模型状态无效,则在提交表单时,我希望用户被重定向到同一页面。但是每当 ModelState 无效时,我都会收到异常

例外

参数字典包含“Bridge.Controllers.ReferrerController”中方法“System.Web.Mvc.ActionResult ReferralConfirmation(Int32)”的不可为空类型“System.Int32”的参数“referralId”的空条目。可选参数必须是引用类型、可空类型或声明为可选参数。 参数名称:参数

型号:

public class ReferrerInstanceViewModel
    {
        [Key]
        public int Id { get; set; }

        [FileType("pdf|doc|docx|PDF", ErrorMessage = "File type is not valid.")]
        [Required]
        public HttpPostedFileBase ProofDoc { get; set; }

        [Required]
        public int ReferralId { get; set; }

        [Required]
        public int? ReferralStatusId { get; set; }
        public IEnumerable<SelectListItem> ReferralStatuses { get; set; }

    }

控制器动作:

    public ActionResult ReferralConfirmation(int referralId)
    {
        var viewModel = new ReferrerInstanceViewModel
        {
            ReferralId = referralId,
            ReferralStatuses = _context.ReferralStatuses.Select(x => new SelectListItem
            {
                Text = x.ReferralStatusType,
                Value = x.ReferralStatusId.ToString()
            })
        };

        return View(viewModel);
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult ReferralConfirmation(ReferrerInstanceViewModel viewModel)
    {
        if (!ModelState.IsValid)
            return RedirectToAction("ReferralConfirmation", viewModel.ReferralId);
       // Some Logic

        return RedirectToAction("ReferrerCenter");
    }

查看:

@using (Html.BeginForm("ReferralConfirmation", "Referrer", null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()
    @Html.HiddenFor(m=>m.ReferralId)

    <div class="form-horizontal">
        <h4>ReferrerInstanceViewModel</h4>
        <hr />
        @Html.ValidationSummary(false, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.ReferralStatusId, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownListFor(model => model.ReferralStatusId, Model.ReferralStatuses, "Reject Or Refer", new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.ReferralId, "*", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.ProofDoc, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextBoxFor(m => m.ProofDoc, new { type = "file" })
                @Html.ValidationMessageFor(model => model.ProofDoc, "*", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

注意事项:

  1. 当 ModelState 无效时,我打电话给RedirectToAction("ReferralConfirmation", I AM PASSING THE REQUIRED ReferralId VALUE HERE)
  2. 当模型状态无效时,即使是 HTTPGET ReferralConfirmation Action 处的断点也不会被命中。

我无法理解这种行为。我在做什么错了。

请注意,在 ModelState 无效后生成的 Url 是

https://localhost:44328/Referrer/ReferralConfirmation

【问题讨论】:

    标签: c# asp.net asp.net-mvc razor asp.net-mvc-5


    【解决方案1】:

    当使用对象调用RedirectToAction 时,您应该传递一个匿名对象,其属性名称与您的 ReferralConfirmation 操作方法的参数名称匹配

    return RedirectToAction("ReferralConfirmation", 
                                       new { referralId = viewModel.ReferralId } );
    

    这将返回一个 302 响应,其位置值为 /ReferralConfirmation?referralId=100,假设 viewModel.ReferralId 中的值为 100

    此外,当模型状态无效时,进行重定向是没有意义的。您应该返回到相同的视图,以便用户可以修复视图并重新提交。

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult ReferralConfirmation(ReferrerInstanceViewModel viewModel)
    {
        if (ModelState.IsValid)
        {
           //to do : Your code before the redirect goes here
    
            return RedirectToAction("ReferralConfirmation", 
                                             new { referralId = viewModel.ReferralId });
        }
        //model validation failed. Let's return to same view
        return View(viewModel);
    }
    

    【讨论】:

    • 哦...我需要传递一个匿名类型。但是先生,为什么我没有得到编译时错误?
    • 因为对于方法,第二个参数类型是对象类型。你传递的是一个有效的对象。所以没有编译器错误。
    • 如果我只返回视图,那么我的推荐ID已经被传递了?
    • 我进行了重定向,以便可以传递referId。
    • 我需要传递“viewModel”还是“viewModel.ReferralId”??
    猜你喜欢
    • 2015-03-21
    • 2015-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 2021-07-15
    相关资源
    最近更新 更多