簡體   English   中英

綁定asp.net MVC4中的下拉列表

[英]bind the drop down list in asp.net MVC4

我有地址的控制器我用它來輸入多個地址但我想創建dropdosnlist來選擇人並輸入他的地址我在我的模型文件夾中創建這個幫助類來創建選擇項

public class PersonsSelectItems
{
    public int SelectedId { get; set; }
    public List<Person> Persons { get; set; }
}

我使用AddressController將selectitem發送到它的視圖

public class AddressController : Controller
    {

        private readonly CustomersDBEntities context = new CustomersDBEntities();
        private PersonsSelectItems personsSelectItems= new PersonsSelectItems();

        ///get list of persones
        /// 
        public List<Person> GetPersonsList()
        {
            return (from c in personsSelectItems.Persons
                    select c).ToList();
        }

        //
        // GET: /Address/

        public ActionResult Index()
        {


            //var model = GetPersonsList(); //var model = GetPersonsList().Select(x => new SelectListItem
            //{
            //    Value = x.PersonID.ToString(),
            //    Text = x.FirstName,
            //    Selected = true | false
            //});

            ///var model = new PersonsSelectItems { Persons = GetPersonsList() };

            var model = GetPersonsList();
            return View(model);
        }

        // 
        // GET: /Address/Welcome/ 

        public string Welcome()
        {
            return "This is the Welcome action method...";
        }

        [HttpPost]
        public ActionResult Create(Address address)
        {
            //Loop through the request.forms

            var Addesslist = new List<Address>();
            for (int i = 1; i <= Request.Form.Count; i++)
            {
                var street = Request.Form["street_0" + i + ""];
                var city = Request.Form["city_0" + i + ""];
                var postalCode = Request.Form["postalCode_0" + i + ""];
                var province = Request.Form["province_0" + i + ""];
                var personID = 1;
                if (street != null && city != null && postalCode != null && province != null)
                {

                    try
                    {
                        context.Addresses.Add(new Address
                        {
                            Street = street,
                            City = city,
                            Province = province,
                            PostalCode = postalCode,
                            PersonID = personID
                        });
                        context.SaveChanges();
                    }
                    catch (Exception exc)
                    {

                    }
                }
                else
                {
                    break;
                }

            }
            return RedirectToAction("Index");
        }
    }

我得到了這個解釋

值不能為空。 參數名稱:source

描述:執行當前Web請求期間發生未處理的異常。 請查看堆棧跟蹤以獲取有關錯誤及其源自代碼的位置的更多信息。

異常詳細信息:System.ArgumentNullException:Value不能為null。 參數名稱:source Adress view @model MVC.Models.Address

Tuple<Person,Order>
@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

@using (Html.BeginForm("Create", "Address", FormMethod.Post))
{

@Html.DropDownListFor(x => x.Person, new SelectList(Model.Person, "PersonId", "FirstName"))



    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

        <div class="table-responsive">
            <table id="address_table" class="table">
                <thead>
                    <tr>
                        <th>Street</th>
                        <th>City</th>
                        <th>Province</th>
                        <th>PostalCode</th>
                        <th>&nbsp;</th>

                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>
                            <input id="Text1" type="text" name="street_01" maxlength="255" required class="street" /></td>
                        <td>
                            <input id="Text2" type="text" name="city_01" maxlength="255" required class="city" /></td>
                        <td>
                            <input id="Text3" type="text" name="province_01" maxlength="255" required class="province" /></td>
                        <td>
                            <input id="Text4" type="text" name="postalCode_01" maxlength="7" required class="postalCode" /></td>
                        <td>&nbsp;</td>
                    </tr>
                </tbody>
            </table>
        </div>
        <input type="button" value="Add Row" id="add_AdressRow" class="btn btn-lg btn-success btn-block" />

        <p>
            <input type="submit" value="Create" />
        </p>

}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

我想問一下如何使用來自AdressController的GetPersonsList()函數中的list來綁定dropDwonList並綁定我現在他們離開去做但我找不到它?

你的問題是你試圖在null列表上使用一些LINQ。

這個壞孩子在這里:

public List<Person> Persons { get; set; }

null 您可以在類型中添加構造函數來初始化它:

public class PersonsSelectItems
{
    public int SelectedId { get; set; }
    public List<Person> Persons { get; set; }

    public PersonsSelectItems() {
        Persons = new List<Person>();
    }
}

..那將阻止你當前的錯誤。

我必須指出一些事情。 首先,命名Persons很奇怪。 使它成為英文復數的People

其次,你實際上不必在這里使用LINQ。 您的GetPersonList方法可以簡單地是:

public List<Person> GetPersonsList()
{
    return personsSelectItems.Persons;
}

即便如此......你已經可以訪問該系列了。 所以你的model分配可以是:

var model = _personsSelectItems.Persons;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM