2011-04-28 104 views
0

我得到这个错误,我不明白我在这里做错了什么。一个错误,我不明白如何修复

这是错误:

传递到词典中的模型项的类型为“System.Data.Objects.ObjectQuery`1 [System.Web.Mvc.SelectListItem]”,但本词典需要一个模型类型为“MyProject.Models.cosmetic”的项目。

这是我贝我的控制器:

public ActionResult Create(string company_id) 
{ 
    IEnumerable<SelectListItem> items = _enteties.companies.Select(x => new SelectListItem() 
    { 
     Value = x.company_id, 
     Text = x.company_id 
    }); 

    return View(items); 

} 

[AcceptVerbs (HttpVerbs.Post)] 
public ActionResult Create([Bind(Exclude = "ID")]cosmetic cosmeticToCreate ,company companyToCreate) 
{ 
    if (!ModelState.IsValid) 
     return View(); 

    try 
    { 
     _repository.Create(cosmeticToCreate,companyToCreate); 
     return RedirectToAction("Index"); 
    } 
    catch 
    { 
     return View(); 
    } 
} 

这是我的看法(请注意我有coametic.model)

@model SrT2.Models.cosmetic 

@{ 
    ViewBag.Title = "Create"; 
    Layout = "~/Views/Shared/_Layout.cshtml"; 
} 

<h2>Create</h2> 

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> 
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> 

@using (Html.BeginForm()) { 
    @Html.ValidationSummary(true) 
    <fieldset> 
    <legend>cosmetic</legend> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.brand) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.brand) 
     @Html.ValidationMessageFor(model => model.brand) 
    </div> 
    @Html.DropDownListFor(model => model.company_id, new SelectList(ViewData["items"] as IEnumerable<SelectListItem>,"Value","Text")) 
@* @Html.DropDownListFor(model => model.company_id, ViewData["items"] as SelectList)*@ 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.product) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.product) 
     @Html.ValidationMessageFor(model => model.product) 
    </div> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.size) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.size) 
     @Html.ValidationMessageFor(model => model.size) 
    </div> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.sale_type) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.sale_type) 
     @Html.ValidationMessageFor(model => model.sale_type) 
    </div> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.price) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.price) 
     @Html.ValidationMessageFor(model => model.price) 
    </div> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.description) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.description) 
     @Html.ValidationMessageFor(model => model.description) 
    </div> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.date) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.date) 
     @Html.ValidationMessageFor(model => model.date) 
    </div> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.company_id) 
    </div> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.category) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.category) 
     @Html.ValidationMessageFor(model => model.category) 
    </div> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.special) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.special) 
     @Html.ValidationMessageFor(model => model.special) 
    </div> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.male_female) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.male_female) 
     @Html.ValidationMessageFor(model => model.male_female) 
    </div> 

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

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

有什么建议?

这是错误: 此属性不能设置为空值。 描述:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪以获取有关该错误的更多信息以及源代码的位置。

异常详细信息:System.Data.ConstraintException:该属性不能设置为空值。

源错误:

线3081:Oncompany_idChanging(值); 3082行:ReportPropertyChanging(“company_id”); 3083行:_company_id = StructuralObject.SetValidValue(value,false); 3084行:ReportPropertyChanged(“company_id”); 3085行:Oncompany_idChanged();

这就是我的代码已经使用:

public ActionResult Create(string company_id) 
    { 

     var model = new SrT2.Models.cosmetic(); 
     //There is No "model.CompanyId " so i have Pass model.cpmpany_id 
     model.company_id = company_id; 
     return View(model); 

    } 


    [AcceptVerbs (HttpVerbs.Post)] 
    public ActionResult Create([Bind(Exclude = "ID")]cosmetic cosmeticToCreate ,company companyToCreate) 
    { 

     if (!ModelState.IsValid) 
      return View(); 

     try 
     { 

      _repository.Create(cosmeticToCreate,companyToCreate); 
      return RedirectToAction("Index"); 
     } 
     catch 
     { 

      return View(); 
     } 

    } 

有何建议?理念?

回答

3

下面是如何使其工作演示...

public ActionResult Create(string company_id) 
{ 
    var model = new SrT2.Models.cosmetic(); 
    model.CompanyId = company_id; 

    return View(model); 

} 

如果创建视图需要一个“整容”为模型传递,你需要确保这是你是什么在您的控制器中执行...

您可以在通过它之前将其添加到模型中,但它必须是正确的类型。

希望这会让你回到正轨。

0

一切都由您的错误和代码说。 _enteties.companies.Select返回通用ObjectQuery,您直接通过查看。但是你的观点说它应该接收类型SrT2.Models.cosmetic,而不是ObjectQuery或者甚至IEnumerable