一个错误我无法理解如何修复

时间:2011-04-28 10:45:57

标签: asp.net-mvc asp.net-mvc-3

我收到此错误,我无法理解我在这里做错了什么。

这是错误:

传递到字典中的模型项的类型为'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(value); 第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();
       }

   }

有什么建议吗?想法?

2 个答案:

答案 0 :(得分:3)

以下是如何使其发挥作用的演示......

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

    return View(model);

}

如果“创建”视图需要将“装饰”作为模型传递,则需要确保这是您在控制器中执行的操作...

您可以在传递之前向模型添加内容,但必须是正确的类型。

希望这会让你回到正轨。

答案 1 :(得分:0)

您的错误和代码都说明了一切。 _enteties.companies.Select返回Generic ObjectQuery,您可以直接将其传递给查看。但是您的观点表明它应该收到SrT2.Models.cosmetic类型,而不是ObjectQueryIEnumerable