具有Ienumerable模型的Asp.net MVC视图在提交时返回null [英] Asp.net mvc view with Ienumerable model returns null on submit

查看:57
本文介绍了具有Ienumerable模型的Asp.net MVC视图在提交时返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习mvc,尝试填充复选框列表并在提交按钮上获取复选框列表的所有选定值,但发布后在控制器中的按钮提交上却为空.view和controller的代码如下.httpget部分运行正常,并根据需要显示所有复选框.但是提交问题发生后

I am learning mvc and try to populate checkbox list and get all selected values of checkbox list on submit button but i get null on button submit in the controller after post.The code for view and controller are as follows. The httpget part is working properly and shows all the checkbox as required. but after submitting problem occurs

查看:

@model IEnumerable<MVCExtra.Models.paymentmethod>

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
@using(Html.BeginForm("Index", "Input", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{ 
    foreach (var item in Model)
    {
        @Html.HiddenFor(x => item.Id);
        @Html.HiddenFor(x => item.Name);
        @Html.CheckBoxFor(x=>item.isselected);
       
        @Html.DisplayFor(x => item.Name);
    }
    <input type="submit" value="submit"/>
}
</body>
</html>

控制器:

 public ActionResult Index()
        {
            List<paymentmethod> listpay = new List<paymentmethod>()
            {
                new paymentmethod() { Id="CS",isselected = true,Name = "Cash"},
                new paymentmethod() { Id="CH",isselected = false,Name = "Cheque"},
                new paymentmethod() { Id="CR",isselected = false,Name = "Credit"},
                new paymentmethod() { Id="BN",isselected = false,Name = "Bank"}

            };
            
            
            return View(listpay);
        }

        [HttpPost]
        public string Index(IEnumerable<paymentmethod> model)
        {
            if (model.Count(x => x.isselected) == 0)
            {
                return "no any option is selected";
            }
            else
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("You selected:");
                foreach (paymentmethod pay in model)
                {
                    if (pay.isselected == true)
                    {
                        sb.Append(":" + pay.Name);
                    }
                }

                return sb.ToString();
            }
        }

推荐答案

您需要使用索引器而不是foreach循环来使模型正确发回

You need to use an indexer not a foreach loop for the model to post back correctly

for(int i=0; i < Model.Count(); i++)
{
    @Html.HiddenFor(x => Model[i].Id);
    @Html.HiddenFor(x => Model[i].Name);
    @Html.CheckBoxFor(x=>Model[i].isselected);

    @Html.DisplayFor(x => Model[i].Name);
}

忘了提及,您需要将模型转换为列表,而不是IEnumerable

Forgot to mention, you'll need to convert your Model to a List rather than an IEnumerable

这篇关于具有Ienumerable模型的Asp.net MVC视图在提交时返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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