我有地址的控制器我用它来输入多个地址但是我想创建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
描述:执行期间发生了未处理的异常 当前的网络请求。请查看堆栈跟踪了解更多信息 有关错误的信息以及它在代码中的起源。
异常详细信息:System.ArgumentNullException:值不能为null。 参数名称:source 地址视图 @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> </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> </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")
}
我想问一下如何使用GetPersonsList()函数中的列表绑定dropDwonList 从AdressController和绑定我现在他们离开了,但我找不到它?
答案 0 :(得分:3)
您的问题是您尝试在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;