在链接到现有对象的数据库中插入对象
本文关键字:对象 数据库 插入 链接 | 更新日期: 2023-09-27 18:00:46
我有一个名为"专业"的对象,我的专业有一个"技能"列表
当我试图为确定的专业插入一项新技能时,我遇到了一个问题
我试着在发布新对象的视图中这样做:
<div>
@Html.ActionLink("Add Skill", "AddNewSkill", new { skill = Model, id = ViewBag.professionalId} )
</div>
控制器方法
public ActionResult AddNewSkill(int professionalId)
{
ViewBag.professionalId = professionalId;
return View();
}
[HttpPost]
public ActionResult AddNewSkill(SkillModel skill, int professionalId)
{
professionalBusiness = new ProfessionalBusiness();
var professional = professionalBusiness.GetById(professionalId);
professional.Skills.Add(skill);
if (ModelState.IsValid)
{
professionalBusiness.Update(professional);
return RedirectToAction("Index");
}
return RedirectToAction("Edit", professionalId);
}
但这更糟糕,因为我的URL只显示了一个参数
http://localhost:20995/Professional/AddNewSkill/1
当我尝试POST时,它会给我一个错误:
[ArgumentException: The parameters dictionary contains a null entry for parameter 'professionalId' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult AddNewSkill(Int32)' in 'TCCApplication.Controllers.ProfessionalController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
Nome do parâmetro: parameters]
System.Web.Mvc.ActionDescriptor.ExtractParameterFromDictionary(ParameterInfo parameterInfo, IDictionary`2 parameters, MethodInfo methodInfo) +607955
System.Web.Mvc.<>c__DisplayClass1.<Execute>b__0(ParameterInfo parameterInfo) +18
System.Linq.WhereSelectArrayIterator`2.MoveNext() +66
System.Linq.Buffer`1..ctor(IEnumerable`1 source) +216
System.Linq.Enumerable.ToArray(IEnumerable`1 source) +77
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +135
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +27
System.Web.Mvc.Async.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41() +28
System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +10
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +57
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +48
System.Web.Mvc.Async.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33() +57
System.Web.Mvc.Async.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49() +223
System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeActionMethodWithFilters>b__36(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +57
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +48
System.Web.Mvc.Async.<>c__DisplayClass2a.<BeginInvokeAction>b__20() +24
System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult) +102
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +57
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +43
System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +14
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +57
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +47
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +25
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +47
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9690164
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
您的参数名称在调用和控制器操作之间不匹配。
尝试将呼叫更改为:
@Html.ActionLink("Add Skill", "AddNewSkill", new { skill = Model, professionalId = ViewBag.professionalId} )
编辑:
使用@Html.ActionLink()
将表单数据张贴到控制器操作是不容易的。我建议您使用标准的html提交按钮将现有的视图html包装在BeginForm语句中:
@using(Html.BeginForm("AddNewSkill","YourControllerName", FormMethod.Post))
{
// ...your existing markup with the properties of the model here...
<button type="submit">Add Skill</button>
}
这应该会将您的所有模型属性发布到控制器操作中。您应该注意,MVC ModelBinder会将各种属性"重新组装"(从技术上讲:绑定)到服务器端的模型。
IOW您不需要尝试将模型作为一个完整的对象发送,就像您试图使用RouteValues一样——就像在中一样
CCD_ 2。
希望能有所帮助。
ActionLink 上提到的Id在哪里
@Html.ActionLink("Add Skill", "AddNewSkill", new { skill = Model, id = ViewBag.professionalId} )
参数名称应与操作结果参数匹配。所以将你的行动链接更改为以下
@Html.ActionLink("Add Skill", "AddNewSkill", new { skill = Model, professionalId = ViewBag.professionalId} )
这应该工作
欢呼