【发布时间】:2014-12-03 21:51:59
【问题描述】:
将数据更新到数据库后出现错误
参数字典包含方法“System.Web.Mvc.ActionResult UpdateChain(Int32, HCIBE.Models.chain)”的不可空类型“System.Int32”的参数“id”的空条目'HCIBE.Controllers.ChainsController'。可选参数必须是引用类型、可空类型或声明为可选参数。
这是我的控制器
[HttpGet]
public ActionResult UpdateChain(int? id)
{
chain objchain = db.chains.Find(id);
if (objchain == null)
{
return HttpNotFound();
}
return View(objchain);
}
[HttpPost]
public ActionResult UpdateChain(int id, [Bind(Include = "name,code,username,password,updated_by,updated_on")] chain chain)
{
chain _objchain = db.chains.Find(id);
try
{
if(ModelState.IsValid)
{
_objchain.code = chain.code;
_objchain.name = chain.name;
_objchain.username = chain.username;
_objchain.password = chain.password;
_objchain.updated_by = Convert.ToInt32("1");
_objchain.updated_on = DateTime.Now;
db.Entry(_objchain).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
}
catch (Exception Ex)
{
ModelState.AddModelError("", "Unable to update data");
}
return View(_objchain);
}
查看
@using (@Html.BeginForm("UpdateChain", "Chains", FormMethod.Post))
{
<div class="form-horizontal">
<hr />
<div class="form-group">
<label class="col-sm-2 control-label">
Select Chain
</label>
<div class="col-md-3">
@Html.DropDownList("ddlchainname", (SelectList)ViewData["chain_name"],"Select Chain", new { onchange = "Action(this.value);", @class = "form-control" })
</div>
<label class="control-label">
or @Html.ActionLink("Add New", "Create")
</label>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">
Chain Name
</label>
<div class="col-md-3">
@Html.TextBox("ChainName", null, new { @class = "form-control" })
</div>
<label class="col-sm-2 control-label">
Username
</label>
<div class="col-md-3">
@Html.TextBox("username", null, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">
Chain Code
</label>
<div class="col-md-3">
@Html.TextBox("ChainCode", null, new { @class = "form-control" })
</div>
<label class="col-sm-2 control-label">
Password
</label>
<div class="col-md-3">
@Html.Password("password", null, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Update" onclick="UpdateChain()" class="btn btn-default" />
</div>
</div>
</div>
}
<script type="text/javascript">
function UpdateChain(){
var _vddlChainID = $("#ddlchainname").val();
alert("Your Selected ID = " + _vddlChainID);
$.ajax({
url: '@Url.Action("UpdateChain", "Chains")',
type: "POST",
data: { "id": _vddlChainID }
});
}
</script>
通过下拉选择我填写所有文本框,当我提交表单时我得到错误。
路由
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
型号
public partial class chain
{
public chain()
{
this.templates = new HashSet<template>();
this.hotels = new HashSet<hotel>();
}
public long chain_id { get; set; }
public string name { get; set; }
public string code { get; set; }
public string username { get; set; }
public string password { get; set; }
public long created_by { get; set; }
public System.DateTime created_on { get; set; }
public Nullable<long> updated_by { get; set; }
public Nullable<System.DateTime> updated_on { get; set; }
public virtual ICollection<template> templates { get; set; }
public virtual ICollection<hotel> hotels { get; set; }
}
【问题讨论】:
-
想显示您的路线?我相信这就是问题所在。默认路由需要一个 ID。
-
您要么省略了一些代码,要么在这里没有多大意义。您为什么要同时使用 AJAX 发布和提交表单(您正在进行 2 个调用 - 一个传递 id 值(AJAX),它什么都不做,另一个传递
chain(SUBMIT)而没有id -
我对 MVC 不太了解,可能是我的工作方式不对,所以请。指导我纠正这一点。谢谢
-
你有很多错误。例如,您正在使用
name="ChainName" but your model does not include a property with the namaeChainName` 渲染一个文本框(至少不是根据您的[Bind(Include= ...)]列表。您的[Bind(Include= ...)]还包括您甚至没有渲染控件的属性。但主要问题是您制作 2对控制器的调用(不需要 javascript)。请发布您的模型,以便我指导您。 -
[Bind(Include=.....)] 在这个上下文中我们传递了什么,Html 控件名还是表格列名?我还用模型更新了我的问题。
标签: c# mysql asp.net entity-framework asp.net-mvc-5