我有一个像这样的数据库模型的简单类
Student.cs
public class Student{
public long Id; //database generated and key.
public string name; //simple name
public virtual StudentData data; //reference to another data
}
StudentData.cs
public class StudentData {
public long Id { get;set; } //database generated and key
public string CityName {get;set; } //simple and dummy data
}
public ActionResult CityName_Read([DataSourceRequest]DataSourceRequest request)
{
IQueryable<City> cities= db.City;
DataSourceResult result = cities.ToDataSourceResult(request, city => new {
Id = city.Id,
CityName = city.CityName
});
return Json(result);
}
我还有StudentData的编辑模板
StudentDataDropdownList.cshtml
@model MyProj.Models.StudentData
@{
Html.Kendo().DropDownListFor(m => m.Id)
.DataValueField("Id")
.DataTextField("CityName")
.DataSource(source =>
{
source.Read("CityName_Read", "City");
});
}
最后,为学生创建页面(假设城市主数据已经可用)
@model MyProj.Models.Student
@{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Student Data</h4>
<hr />
@Html.ValidationSummary(true)
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-5">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
@Html.LabelFor(model => model.StudentData)
<div class="col-md-5">
@Html.EditorFor(model => model.StudentData, "StudentDataDropdownList")
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
结果是名称文本框正在显示,但我想要的那个下拉列表不存在。就像它没有被召唤一样。有什么我想念的吗?
我不想将ViewModel用于此数据,因为除了StudentData之外的所有其他数据都只是简单的字符串或长。