asp.net-mvc – Asp.Net MVC:为什么我的视图将NULL模型传递给我的控制器?
发布时间:2020-05-23 03:59:55 所属栏目:asp.Net 来源:互联网
导读:我无法弄清楚为什么我的视图只将模型的NULL传回给我的控制器. 这适用于Edit Post方法.我使用Edit Post方法检查了其他控制器,其结构与此相同,并且它们工作正常.它似乎只是这个视图和控制器. 这是我的观点: @model Non_P21_Quote_System_v1._0.Models.gl_code@
|
我无法弄清楚为什么我的视图只将模型的NULL传回给我的控制器.
这是我的观点: @model Non_P21_Quote_System_v1._0.Models.gl_code
@{
ViewBag.Title = "Edit";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Edit</h2>
@if (TempData["Message"] != null)
{
<div style="color:green">
@TempData["Message"]
</div><br />
}
@if (ViewBag.error != null)
{
<div style="color:red">
<h3>@ViewBag.error</h3>
</div><br />
}
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>gl_code</h4>
<hr />
@Html.ValidationSummary(true,"",new { @class = "text-danger" })
@Html.HiddenFor(model => model.ID)
<div class="form-group">
@Html.LabelFor(model => model.GL_code,"GL Code",htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.GL_code,new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.GL_code,new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.GL_description,"Gl Description",htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.GL_description,new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.GL_description,new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.expense_type_ID,"Expense",htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("expense_type_ID",null,htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.expense_type_ID,new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.eag,"Employee Account Group",htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("eag",htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.eag,new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List","gl_Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
这是我的控制器方法: [HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Edit([Bind(Include = "ID,GL_code,GL_description,expense_type_ID,eag")] gl_code gl_code)
{
if (ModelState.IsValid)
{
db.Entry(gl_code).State = EntityState.Modified;
await db.SaveChangesAsync();
return RedirectToAction("gl_Index");
}
ViewBag.eag = new SelectList(db.employee_account_group,"ID","eag_name");
ViewBag.expense_type_ID = new SelectList(db.expense_type,"type",gl_code.expense_type_ID);
return View(gl_code);
}
当我调试它时,我看到传入的模型的值为NULL.我在控制器端看到这个在Edit方法的参数部分. 解决方法它为null,因为您的模型包含名为gl_code的属性,并且您还在POST方法中为模型gl_code命名了参数.更改其中一个的名称,模型将正确绑定. 内部发生的是表单为每个成功的表单控件提交名称/值对,在您的情况下为gl_code = someValue. DefaultModelBinder首先初始化模型的新实例.然后,它读取表单值并找到模型中属性的匹配项,并将其设置为someValue.但是它也在方法参数中找到匹配并尝试将参数的值设置为someValue,这会失败(因为你不能做gl_code gl_code =“someValue”;)并且模型变为null. (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc-3 – 任何好的样品开始使用Dapper?
- asp.net-mvc – 使用URL重写时Url.Action不正确
- asp.net-mvc-4 – 如何在iframe src中使用Url.Action
- asp.net – 引用依赖于ConfigurationManager的.net core 2中
- ASP.NET内置用户配置文件与旧stile用户类/表
- asp.net – web.config中的多个/不同的身份验证设置
- asp.net-mvc – 提交表单并将数据传递给FileStreamResult类
- 从ASP.NET MVC应用程序中的Amazon SES发送电子邮件
- asp.net核心 – 在Visual Sudio 2015中的Aurelia项目设置和
- 在ASP.NET中使用JQuery选择ID属性
推荐文章
站长推荐
- asp.net – IIS重写输入类型
- webforms – ASP.Net Core 1.0是否支持WebForm项
- asp.net – 如何打破VB.NET中的“if”块
- asp.net – 连接到源文件XYZ的撤消管理器时出错
- asp.net-mvc – DropDown列表onchange事件和MVC中
- ASP.NET MVC Web应用程序中的控制器是否应该调用
- asp.net – 文件下载问题:文件名与空格被截断!
- ASP.NET上的ASP.NET未来?
- asp.net-mvc – 如何使用具有可空类型的强类型HT
- asp.net – aspx中__EVENTVALIDATION __VIEWSTAT
热点阅读
