Asp.net mvc 3 提交后是否将空值传递给控制器中的参数?

Asp.net mvc 3 提交后是否将空值传递给控制器中的参数?,asp.net-mvc-3,Asp.net Mvc 3,下面的代码是我键入的示例 在我将传递空值提交给控制器之后,在控制器中我使用了类名,然后正确地传递了值,但当我使用参数时,它将传递空值给控制器。请给我一个解决办法 控制器: [HttpGet] public ActionResult Index() { return View(); } [HttpPost] public ActionResult Index(string firstname

下面的代码是我键入的示例 在我将传递空值提交给控制器之后,在控制器中我使用了类名,然后正确地传递了值,但当我使用参数时,它将传递空值给控制器。请给我一个解决办法

控制器:

[HttpGet]
        public ActionResult Index()
        {
            return View();
        }


        [HttpPost]
        public ActionResult Index(string firstname)
        {
            LogonViewModel lv = new LogonViewModel();
            var ob = s.Newcustomer(firstname)

            return View(ob );
        }
[HttpPost]
public ActionResult Index(LogonViewModel model) {
    if (ModelState.IsValid) {
      // TODO: Whatever logic is needed here!
    }
    return View(model);
}
[HttpGet]
        public ActionResult Index()
        {
            return View();
        }


        [HttpPost]
        public ActionResult Index(IList<LogonViewModel> obj)
        {

            LogonViewModel lv = new LogonViewModel();
            var ob = lv.Newcustomer(obj[0].FirstName)

            return View(ob );
        }
视图:

@model-IList
@{
ViewBag.Title=“Index”;
}
@使用(Html.BeginForm())
{
对于(int i=0;i<1;i++)
{  
@Html.LabelFor(m=>m[i].UserName)
@Html.TextBoxFor(m=>m[i].UserName)
@Html.ValidationMessageFor(per=>per[i].UserName)
}
}
型号:

 public class LogonViewModel
    {
        [Required(ErrorMessage = "User Name is Required")]
        public string UserName { get; set; }
    }



    public List<ShoppingClass> Newcustomer(string firstname1)
        {

            List<ShoppingClass> list = new List<ShoppingClass>();
           ..
        }
public类LogonViewModel
{
[必需(ErrorMessage=“需要用户名”)]
公共字符串用户名{get;set;}
}
公共列表Newcustomer(字符串firstname1)
{
列表=新列表();
..
}
这是:

[HttpGet]
public ActionResult Index() {
    return View();
}
在您看来,这并不意味着:

@model IList<clientval.Models.LogonViewModel>
您没有向视图发送模型,并且在视图中使用列表,但希望控制器中有一个字符串值。您的示例或代码有一些非常奇怪/错误

为什么你有一个IList作为你的模型?如果您只需要使用一个输入字段呈现表单。您应该有如下代码:

[HttpGet]
public ActionResult Index() {
    return View(new LogonViewModel());
}
以及以下观点:

@model clientval.Models.LogonViewModel
@using (Html.BeginForm())
{
    @Html.LabelFor(m => m.UserName)
    @Html.TextBoxFor(m => m.UserName)
    @Html.ValidationMessageFor(m => m.UserName)

    <input type="submit" value="submit" />
}

它起作用了。。我已更改控制器,如下所示

控制器:

[HttpGet]
        public ActionResult Index()
        {
            return View();
        }


        [HttpPost]
        public ActionResult Index(string firstname)
        {
            LogonViewModel lv = new LogonViewModel();
            var ob = s.Newcustomer(firstname)

            return View(ob );
        }
[HttpPost]
public ActionResult Index(LogonViewModel model) {
    if (ModelState.IsValid) {
      // TODO: Whatever logic is needed here!
    }
    return View(model);
}
[HttpGet]
        public ActionResult Index()
        {
            return View();
        }


        [HttpPost]
        public ActionResult Index(IList<LogonViewModel> obj)
        {

            LogonViewModel lv = new LogonViewModel();
            var ob = lv.Newcustomer(obj[0].FirstName)

            return View(ob );
        }
[HttpGet]
公共行动结果索引()
{
返回视图();
}
[HttpPost]
公共行动结果索引(IList obj)
{
LogonViewModel lv=新LogonViewModel();
var ob=lv.Newcustomer(obj[0].FirstName)
返回视图(ob);
}

举个例子,我键入了这个,但实际上我在控制器中传递了许多参数……如果示例与实际代码不太接近,并且您的示例很奇怪,那么很难发现错误。