为什么List< SelectListItem>post时变为null

本文关键字:null post List SelectListItem 为什么 | 更新日期: 2023-09-27 18:07:51

我试图在Asp中显示一个RadioButtonList。Net MVC 3.0。这是我的扩展方法:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Linq.Expressions;
using System.Text;
public static class HtmlExtensions
{
    public static MvcHtmlString RadioButtonForSelectList<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper,
        Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList)
    {
        StringBuilder stringBuilder = new StringBuilder();
        var metaData = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
        if (selectList != null)
        {
            foreach (var item in selectList)
            {
                var id = string.Format("{0}_{1}", metaData.PropertyName, item.Value);
                var label = htmlHelper.Label(id, HttpUtility.HtmlEncode(item.Text));
                var radio = htmlHelper.RadioButtonFor(expression, item.Value, new { id = id });
                stringBuilder.AppendFormat("<div class='"RadioButton'">{0}{1}</div>",radio.ToHtmlString(),label.ToHtmlString());
            }
        }
        return new MvcHtmlString(stringBuilder.ToString());
    }
}

这些是我的HomeController动作方法:

public ActionResult Index()
{
    return View(GetIndexViewModel());
}
private IndexViewModel GetIndexViewModel()
{
    List<SelectListItem> list = new List<SelectListItem> 
    {
        new SelectListItem{ Selected = true, Value = "1", Text = "Sunday" },
        new SelectListItem{ Selected = false, Value = "2", Text = "Monday" },
        new SelectListItem{ Selected = false, Value = "3", Text = "Tuesday" },
        new SelectListItem{ Selected = false, Value = "4", Text = "Wednesday" },
        new SelectListItem{ Selected = false, Value = "5", Text = "Thursday" },
        new SelectListItem{ Selected = false, Value = "6", Text = "Friday" },
        new SelectListItem{ Selected = false, Value = "7", Text = "Saturday" },
    };
    return new IndexViewModel { SelectLists = list, SelectedItem = list[0] };
}
[HttpPost]
public ActionResult Index(IndexViewModel indexViewModel)
{
    return View(GetIndexViewModel()); //this works
    return View(indexViewModel); //why this doesn't work??
}

This is my index.cshtml:

@model IndexViewModel    
@using (Html.BeginForm())
{
    <h2>Select any one item</h2>
    @Html.RadioButtonForSelectList(model => model.SelectedItem.Value, Model.SelectLists)
    <br />
    <input type="submit" value="Submit" />
}

我已经评论了哪个有效,哪个无效。基本上我的List变成了空。我做错了什么?如果我必须每次都这样做,那么最好使用webforms,因为webforms可以在回发时保存数据。使用MVC基本上意味着我必须使数据库调用每次的值被张贴。我的代码是正确的还是有一些错误?

为什么List< SelectListItem>post时变为null

因为您在GET操作中绑定了内容并不意味着它将在POST操作中绑定到相同的属性。默认的模型绑定器只绑定它在请求中看到的内容。因此,如果这些信息在发布时没有发送到服务器,它将丢失。

您的自定义助手生成了一堆单选按钮和标签,但我看不到您在任何地方为文本属性生成输入字段。因此,他们将会失去。

也就是说,您的帮助器看起来是正确的,如果您想重新显示相同的视图,您应该在POST操作中简单地从最初获取的数据存储中重新获取该信息。因此,正确的方法如下:

[HttpPost]
public ActionResult Index(IndexViewModel IndexViewModel)
{
    return View(GetIndexViewModel());
}