使用部分视图在mvc中绑定下拉列表 [英] Bind Dropdown in mvc using Partial View

查看:84
本文介绍了使用部分视图在mvc中绑定下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用局部视图绑定下拉列表我的代码是

对于部分视图

i am trying to bind a dropdown using partial view my code is
For partial View

@model TenderKhabarCRM.Models.Status

{ 
    @Html.DropDownList("status")
    
}



在视图中


in view

<tr>
                <td>
                    Status
                </td>
                <td>
                    :
                </td>
                <td>
                    <div id="Status">@Html.Partial("StatusUserControl", Model)</div>
                </td>
            </tr>


控制器中的
我有


in controller I have

 public ActionResult Index()
        {
            List<TenderKhabarCRM.Models.Status> status = new List<TenderKhabarCRM.Models.Status>();
            status = TenderKhabarCRM.DAL.DAL.GetAllStatus();
            ViewBag.StatusList = new SelectList(status, "StatusID", "StatusName");
 return PartialView(status);
        }

public static List<Status> GetAllStatus()
        {
            SqlDataReader reader = null;
            SqlParameter[] sqlParameter = new SqlParameter[] { };
            List<Status> statusList = new List<Status>();
            try
            {
                reader = DalSqlHelper.ExecuteReader(connectionString, CommandType.StoredProcedure, "spGetAllStatus", sqlParameter);

                while (reader.Read())
                {
                    TenderKhabarCRM.Models.Status status = MapStatusObject(reader);
                    statusList.Add(status);
                }
            }
            finally
            {
                if (reader != null && !reader.IsClosed)
                {
                    reader.Close();
                    reader = null;
                }
            }
            return statusList;
        }



但在部分视图中出现错误没有类型为'IEnumerable< SelectListItem>'的ViewData项具有键'status'。我只是无法理解它是什么可以任何人帮助.....


but in partial view an error comes There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'status'. i just can't understand what it is can any one help.....

推荐答案

我已经创建了一个工作正常的样本。请尝试这样做/>
I have created an sample which works fine.Please try like that
public class TestController : Controller
    {
        //
        // GET: /Test/

        public ActionResult Index()
        {

            ViewBag.StatusList =
                           DropDownList<Status>.LoadItems(GetData(), "StatusId", "Name");
            return View();
        }
        public ActionResult _PartialView()
        {
            return PartialView();
        }
       public List<Status>  GetData()
       {
           var list = new List<Status> {new Status() {StatusId = 1, Name = "AAA"}};
           return list;
       }
    }
    public class  Status
    {
        public decimal StatusId { get; set; }
        public string Name { get; set; }
    }
    public class MyViewModel
    {
        public int StatusId { get; set; }
    }
    public static class DropDownList<T>
    {
        public static SelectList LoadItems(IList<T> collection, string value, string text)
        {
            return new SelectList(collection, value, text);
        }
    }





部分查看代码



Partial View code

@model MvcApplication1.Controllers.MyViewModel

 @Html.DropDownListFor(model => model.StatusId, (IEnumerable<SelectListItem>)ViewBag.StatusList, "--Select--", new { @class = "select" })





索引视图



Index View

@model MvcApplication1.Controllers.MyViewModel
@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>
@Html.Partial("_PartialView")





注意:请确保部分视图应位于View>>测试>内;>文件夹

希望这有帮助



Note: Please make sure that partial view should be inside of View>>Test>> folder
Hope this helps


访问这里......

http://forums.asp.net/t/1759232.aspx [ ^ ]







http:// stackoverflow.com/questions/5188563/asp-net-mvc-drop-down-list-selection-partial-views-and-model-binding [ ^ ]
Visit Here...
http://forums.asp.net/t/1759232.aspx[^]

or

http://stackoverflow.com/questions/5188563/asp-net-mvc-drop-down-list-selection-partial-views-and-model-binding[^]


这篇关于使用部分视图在mvc中绑定下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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