2011-04-28 23 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