在链接到现有对象的数据库中插入对象

时间:2014-04-30 02:35:50

标签: c# asp.net-mvc

我有一个名为“专业”的对象,我的专业人士有一个“技能”列表

当我尝试向确定的专业人员插入新技能时,我遇到了问题

我试图在发布新对象的视图中执行此操作:

<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);

        }

但这很奇怪,因为我的网址只显示了一个参数

  

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

2 个答案:

答案 0 :(得分:1)

您的参数名称在呼叫和控制器操作之间不匹配。

尝试将通话更改为:

@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一样 - 如

new { skill = Model...

希望有所帮助。

答案 1 :(得分:0)

通过ActionLink

提及的您的ID在哪里
@Html.ActionLink("Add Skill", "AddNewSkill", new { skill = Model, id = ViewBag.professionalId} ) 

参数名称应与操作结果参数匹配。所以改变你的行动链接如下

@Html.ActionLink("Add Skill", "AddNewSkill", new { skill = Model, professionalId = ViewBag.professionalId} ) 

这应该有效

欢呼声