我在控制器中使用EF创建dropdosnlist时遇到问题我必须获取具有名称和ID的人员列表才能绑定它dropdosnlist只是想从控制器代码中获取dropdosnlist的最佳方法是什么?
person model
using System;
using System.Collections.Generic;
public partial class Person
{
public Person()
{
this.Addresses = new HashSet<Address>();
}
public int PersonID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string MiddleName { get; set; }
public System.DateTime DateOfBirth { get; set; }
public string Gender { get; set; }
public virtual ICollection<Address> Addresses { get; set; }
}
public class AddressController : Controller
{
private readonly CustomersDBEntities context = new CustomersDBEntities();
///get list of persones
///
public List<Person> GetPersonsList()
{
return (from c in context.Persons
select c).ToList();
}
//
// GET: /Address/
public ActionResult Index()
{
var model = GetPersonsList().Select(x =>new SelectListItem
{
Value = x.PersonID.ToString(),
Text = x.FirstName,
Selected = true | false
}).ToList();
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");
}
}
Compiler Error Message: CS0103: The name 'personList' does not exist in the current context
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@using (Html.BeginForm("Create", "Address", FormMethod.Post))
{
@Html.DropDownListFor(m =>m.Person,personList);
@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")
}
更新错误
描述:编译服务此请求所需的资源时发生错误。请查看以下特定错误详细信息并相应地修改源代码。
编译器错误消息:CS1061:'MVC.Models.Address'不包含'Persons'的定义,也没有扩展方法'Persons'接受类型'MVC.Models.Address'的第一个参数可以找到(是你错过了使用指令或程序集引用?)
答案 0 :(得分:0)
为什么不创建一个用于构建下拉列表的模型(或使用您拥有的模型)并传递给查看?
public class PersonModel
{
public int SelectedId {get;set;}
public List<Person> Persons {get; set;}
}
调用此方法并将模型传递给您的视图
public List<Person> GetPersonsList()
{
return (from c in context.Persons
select c).ToList();
}
//
// GET: /Address/
public ActionResult Index()
{
var model = new PersonModel{ Persons = GetPersonsList()};
return View(model);
}
然后在您的视图中构建列表:
@Html.DropDownListFor(x => x.SelectedId, new SelectList(Model.Persons, "PersonId", "FirstName"))