从视图中传递时,在控制器接收缺少参数 [英] Missing parameter received in the controller when passing from view

查看:192
本文介绍了从视图中传递时,在控制器接收缺少参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我包括我的asp.net mvc4认为某些机型,所以我必须创建一个包含两个其他车型基本视图模型:

I have include some models in my asp.net mvc4 view so I have create a base view model which contains the two other models:

namespace MyNamespace.Models
{
    public class CustomViewModel
    {
        public FirstTypeModel FirstViewModel { get; set; }
        public SecondTypeModel SecondViewModel { get; set; }
    }
}

和视图:

 @model MyNamespace.Models.CustomViewModel

 @using (Html.BeginForm("AddFields", "Configure", FormMethod.Post))
 { 
         (...)
                 <div id="componentId">
                     @Html.LabelFor(m => m.FirstViewModel.SelectedCompTypeId, new { @id = "componentIdLabel" })
                     @Html.DropDownListFor(m => m.FirstViewModel.SelectedCompTypeId, Model.FirstViewModel.CompTypeItems, new { @name = "SelectedCompTypeId", @id = "componentType" })
                 </div>
         (...)

                 <input id="submitAddComp" type="submit" value="@Resource.ButtonTitleAddComponent" />

 }

在我的控制器:

public ActionResult AddFields(string param1, string param2, string param3, int selectedCompTypeId)
{
 ...
}

在提交按钮我得到selectedCompTypeId为空点击(参数1,参数2和参数3正确传递),但如果我看下面的请求从控制器中有正确的值:

when click on the submit button I am getting selectedCompTypeId as null (param1, param2 and param3 are passed correctly), but if I watch below request from within the controller it has the correct value:

Request["FirstViewModel.SelectedCompTypeId"]

因此​​,如何正确的参数传递给控制器​​,以selectedCompTypeId不为空?

so how to pass the correct parameter to the controller in order to selectedCompTypeId not to be null?

注:其中只有一种模式,创建包含其他两个示范基地之前,它正常工作。之前,兰巴前pression为m => m.SelectedCompTypeId代替M => m.FirstViewModel.SelectedCompTypeId。

Note: including only one model, before creating the base model which contains the others two, it was working correctly. Before, lamba expression was m => m.SelectedCompTypeId instead m => m.FirstViewModel.SelectedCompTypeId.

推荐答案

添加一个构造函数初始化你的第一和第二模式。

Add a constructor initializing your first and second model.

namespace MyNamespace.Models
{
    public class CustomViewModel
    {
        public CustomViewModel()
        {
            FirstViewModel = new FirstTypeModel();
            SecondViewModel = new SecondTypeModel();
        }

        public FirstTypeModel FirstViewModel { get; set; }
        public SecondTypeModel SecondViewModel { get; set; }
    }
}

编辑:
但是,强似所有参数一个接一个,只要把模型本身在AddFields行动。你所面临的问题是,当你使用 DropDownListFor 参数的名称是FirstViewModel.SelectedCompTypeId,而不仅仅是SelectedCompTypeId为你拥有了它在您的控制器

But instead of passing all parameters one by one, just put the Model itself in your AddFields action. The problem you are facing, is that as you are using DropDownListFor the name of the parameter is "FirstViewModel.SelectedCompTypeId" and not just "SelectedCompTypeId" as you have it in your controller.

有2个选择,一个比另一个更好的:

There are 2 options, one better than the other:

选项1:

而不是使用

public ActionResult AddFields(string param1, string param2, string param3, int selectedCompTypeId)
{
 ...
}

使用这种方式。

public ActionResult AddFields(CustomViewModel model)
{
 ...
}

这是更好的,因为如果你添加更多的字段的明天,你并不需要改变行为的签名,并且绑定是由框架完成。

this is better, because if you add more fields tomorrow, you don't need to change actions signature, and the binding is done by the framework.

选项2:更改 DropDownListFor 的DropDownList ,这样一来,可以说这是参数的名称,并使其发挥作用。这是迄今为止preferrable第一个选项...是清洁的。

OPTION 2: Change the DropDownListFor for a DropDownList, and this way, you can say which is the name of the parameter, and make it work. It is far preferrable the first option... is cleaner.

@Html.DropDownList("selectedCompTypeId", theList, "Select one...", new { @class = "myNiceCSSStyle"})

这篇关于从视图中传递时,在控制器接收缺少参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆