简体   繁体   中英

Issue when creating a reference of a Model without using Html.EditorFor() asp.net mvc

I am facing an issue when trying to create a reference of my model Restriction:

    public int RestrictionID
    public string portefeuille
    public int AssetID
    public int SegmentID
    public int SubAssetID
    public int Min
    public int Max
    public string logic_op
    public virtual Asset Asset
    public virtual Segment Segment
    public virtual SubAsset SubAsset

That is instead of using the normal template in create view with @Html.EditorFor I am using a dropdown list with a script behind to fill the dropdown depending on the previous selected item that's work well, but when submitting nothing happen no error no redirecting, and of course the reference is not added to the database. Here is my Create view:

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

    @Html.DropDownList("Asset", ViewBag.AssetID as SelectList, "Select a Asset Class", new { id="Asset" })<br />
            <select id="Segment" name="segment"></select><br />   //Here I am not using Html.EditorFor as EF does
            <select id="subAsset" name="SubAsset"></select><br />

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

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

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

and my RestrictionController: ( I omitted useless parts)

    public ActionResult Create()
    {
        ViewBag.AssetID = new SelectList(db.Assets, "AssetID", "Asset_Name");

        return View();
    }

    //
    // POST: /Restriction/Create

    [HttpPost]
    public ActionResult Create(Restriction restriction)
    {
        if (ModelState.IsValid)
        {
            db.Restrictions.Add(restriction);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        ViewBag.AssetID = new SelectList(db.Assets, "AssetID", "Asset_Name", restriction.AssetID);

        return View(restriction);
    }

Can somebody help to find where is the problem ?

Thank you for your help!

Name of the property in the model a should be same as name of the element in the html in order to bind values after the post back.That is why the code below

 <select id="Segment" name="segment"></select><br />   //Here I am not using Html.EditorFor as EF does
            <select id="subAsset" name="SubAsset"></select><br />

should be replaced by.

 <select id="Segment" name="SegmentID"></select><br />   //Here I am not using Html.EditorFor as EF does
            <select id="subAsset" name="SubAssetID"></select><br />

Always look at the HTML generated by any technology. This will help you to understand how the things are working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM