我有一个名为“语言”的父表,它存储一个人的语言技能(比如他流利,好,好......)
我有一个子表“Manpower”,其中2个外键指向不同语言的同一个表(英语和阿拉伯语)。
现在,当我尝试创建一个新的人力资源时,它总是返回ModelState对该语言无效。
错误是“从类型'System.String'到类型'AlTmaizMVC3.Models.Language'的参数转换失败,因为没有类型转换器可以在这些类型之间进行转换。”
控制器:
[Authorize]
public ActionResult Create()
{
ViewBag.CategoryId = new SelectList(db.Categories.OrderBy(r => r.Name), "Id", "Name");
ViewBag.CountryId = new SelectList(db.Countries.OrderBy(r => r.Name), "Id", "Name");
ViewBag.MaritalStatusId = new SelectList(db.MaritalStatus.OrderBy(r => r.Name), "Id", "Name");
ViewBag.ReligionId = new SelectList(db.Religions.OrderBy(r => r.Name), "Id", "Name");
ViewBag.EnglishID = new SelectList(db.Languages , "Id", "Name");
ViewBag.ArabicID = new SelectList(db.Languages, "Id", "Name");
return View("~/Views/en/Create.cshtml");
}
[HttpPost]
[Authorize]
public ActionResult Create(Manpower manpower)
{
if (ModelState.IsValid)
{
manpower.CreatedBy = User.Identity.Name;
manpower.CreatedOn = DateTime.Now;
db.Manpowers.AddObject(manpower);
db.SaveChanges();
return RedirectToAction("AddImage", new { manpower.Id});
}
ViewBag.CategoryId = new SelectList(db.Categories.OrderBy(r => r.Name), "Id", "Name", manpower.CategoryId);
ViewBag.CountryId = new SelectList(db.Countries.OrderBy(r => r.Name), "Id", "Name", manpower.CountryId);
ViewBag.MaritalStatusId = new SelectList(db.MaritalStatus.OrderBy(r => r.Name), "Id", "Name", manpower.MaritalStatusId);
ViewBag.ReligionId = new SelectList(db.Religions.OrderBy(r => r.Name), "Id", "Name", manpower.ReligionId);
ViewBag.EnglishID = new SelectList(db.Languages, "Id", "Name", manpower.EnglishID);
ViewBag.ArabicID = new SelectList(db.Languages, "Id", "Name", manpower.ArabicID);
return View("~/Views/en/Create.cshtml", manpower);
}
查看
<tr>
<td>@Html.LabelFor(model => model.MaritalStatusId, "Marital Status")</td>
<td>:</td>
<td>@Html.DropDownList("MaritalStatusId", String.Empty)<br />
@Html.ValidationMessageFor(model => model.MaritalStatusId)</td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>@Html.LabelFor(model => model.EnglishID, "English")</td>
<td>:</td>
<td>@Html.DropDownList("EnglishID", String.Empty)<br />
@Html.ValidationMessageFor(model => model.EnglishID)</td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>@Html.LabelFor(model => model.ArabicID, "Arabic")</td>
<td>:</td>
<td>@Html.DropDownList("ArabicID", String.Empty)<br />
@Html.ValidationMessageFor(model => model.ArabicID)</td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
语言表模型
[EdmEntityTypeAttribute(NamespaceName="AlTmaizDBModel", Name="Language")]
[Serializable()]
[DataContractAttribute(IsReference=true)]
public partial class Language : EntityObject
{
#region Factory Method
/// <summary>
/// Create a new Language object.
/// </summary>
/// <param name="id">Initial value of the Id property.</param>
public static Language CreateLanguage(global::System.Int16 id)
{
Language language = new Language();
language.Id = id;
return language;
}
#endregion
#region Primitive Properties
/// <summary>
/// No Metadata Documentation available.
/// </summary>
[EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
[DataMemberAttribute()]
public global::System.Int16 Id
{
get
{
return _Id;
}
set
{
if (_Id != value)
{
OnIdChanging(value);
ReportPropertyChanging("Id");
_Id = StructuralObject.SetValidValue(value);
ReportPropertyChanged("Id");
OnIdChanged();
}
}
}
private global::System.Int16 _Id;
partial void OnIdChanging(global::System.Int16 value);
partial void OnIdChanged();
/// <summary>
/// No Metadata Documentation available.
/// </summary>
[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
[DataMemberAttribute()]
public global::System.String Name
{
get
{
return _Name;
}
set
{
OnNameChanging(value);
ReportPropertyChanging("Name");
_Name = StructuralObject.SetValidValue(value, true);
ReportPropertyChanged("Name");
OnNameChanged();
}
}
private global::System.String _Name;
partial void OnNameChanging(global::System.String value);
partial void OnNameChanged();
/// <summary>
/// No Metadata Documentation available.
/// </summary>
[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
[DataMemberAttribute()]
public global::System.String NameAR
{
get
{
return _NameAR;
}
set
{
OnNameARChanging(value);
ReportPropertyChanging("NameAR");
_NameAR = StructuralObject.SetValidValue(value, true);
ReportPropertyChanged("NameAR");
OnNameARChanged();
}
}
private global::System.String _NameAR;
partial void OnNameARChanging(global::System.String value);
partial void OnNameARChanged();
#endregion
#region Navigation Properties
/// <summary>
/// No Metadata Documentation available.
/// </summary>
[XmlIgnoreAttribute()]
[SoapIgnoreAttribute()]
[DataMemberAttribute()]
[EdmRelationshipNavigationPropertyAttribute("AlTmaizDBModel", "FK_Manpower_Language", "Manpower")]
public EntityCollection<Manpower> Manpowers
{
get
{
return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedCollection<Manpower>("AlTmaizDBModel.FK_Manpower_Language", "Manpower");
}
set
{
if ((value != null))
{
((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedCollection<Manpower>("AlTmaizDBModel.FK_Manpower_Language", "Manpower", value);
}
}
}
/// <summary>
/// No Metadata Documentation available.
/// </summary>
[XmlIgnoreAttribute()]
[SoapIgnoreAttribute()]
[DataMemberAttribute()]
[EdmRelationshipNavigationPropertyAttribute("AlTmaizDBModel", "FK_Manpower_Language1", "Manpower")]
public EntityCollection<Manpower> Manpowers1
{
get
{
return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedCollection<Manpower>("AlTmaizDBModel.FK_Manpower_Language1", "Manpower");
}
set
{
if ((value != null))
{
((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedCollection<Manpower>("AlTmaizDBModel.FK_Manpower_Language1", "Manpower", value);
}
}
}
#endregion
}
请帮忙。 感谢
答案 0 :(得分:4)
你应该使用Model
,而不是ViewBag
,这是你头痛的开始!
首先让我们根据您的需要创建一个模型:
public class MyModel
{
public Manpower Manpower { get; set; }
public SelectList Categories { get; set; }
public SelectList MaritalStatuses { get; set; }
public SelectList Religions { get; set; }
public SelectList Languages { get; set; }
//here's your Load Method to populate it
public MyModel Load()
{
//load your Manpower object here
Categories = new SelectList(db.Categories
.OrderBy(x => x.Name)
.ToList(), "Id", "Name");
Countries = new SelectList(db.Countries
.OrderBy(x => x.Name)
.ToList(), "Id", "Name");
MaritalStatuses = new SelectList(db.MaritalStatus
.OrderBy(x => x.Name)
.ToList(), "Id", "Name");
Religions = new SelectList(db.Religions
.OrderBy(x => x.Name)
.ToList(), "Id", "Name");
Languages = .new SelectList(db.Languages
.OrderBy(x => x.Name)
.ToList(), "Id", "Name");
}
}
现在让我们修复一下get controller方法:
[Authorize]
public ActionResult Create()
{
var model = new MyModel().Load();
return View("~/Views/en/Create.cshtml", model);
}
接下来,我将向您展示示例查看代码(适当格式化),请注意我在这里假设Manpower拥有所有可能的字段:
@model MyModel //make sure this is the top line of your view
@Html.DropDownListFor(m => m.Manpower.CategoryId, m.Categories)
@Html.DropDownListFor(m => m.Manpower.MaritalStatusId, m.MaritalStatuses)
@Html.DropDownListFor(m => m.Manpower.ReligionId, m.Religions)
@Html.DropDownListFor(m => m.Manpower.EnglishId, m.Languages)
@Html.DropDownListFor(m => m.Manpower.ArabicId, m.Languages)
//your other Manpower object items here now....
现在,我们可以在Save方法中执行:
[HttpPost]
[Authorize]
public ActionResult Create(MyModel model)
{
if (ModelState.IsValid)
{
model.Manpower.CreatedBy = User.Identity.Name;
model.Manpower.CreatedOn = DateTime.Now;
db.Manpowers.AddObject(model.Manpower);
db.SaveChanges();
return RedirectToAction("AddImage", new { manpower.Id});
}
return RedirectToAction("Create"); //not sure why you're going back to create though?
}