Reputation: 225
I am trying to get the selected value of three properties of my model, using dropdownlist that populate the next dropdownlist using a script.
So my problem is that I replace the EF code by this:
@Html.DropDownList("AssetID", ViewBag.AssetID as SelectList, "Select a Asset Class", new { id="AssetID" })<br />
<select id="Segment" name="segment"></select><br />
<select id="subAsset" name="SubAsset"></select><br />
Instead of that code that EF gives:
@Html.DropDownList("AssetID", String.Empty)
@Html.DropDownList("SegmentID", String.Empty)
@Html.DropDownList("SubAssetID", String.Empty)
That are three properties (foreign key) of my Model Restriction. My problem is that now the modelState is not valid and thus the reference of Restriction is not added to the database, maybe I have to DropDownlistFor to bind the selected value to the property bu I don't know how. Also, I can post the script if needed.
My Model Restriction:
public string portefeuille
public int AssetID
public int SegmentID
public int SubAssetID
public int Min
public int Max
My Script for populating dropdown based on previous selected item:
$(function () {
$('#AssetID').change(function () {
$.getJSON('/Restriction/SegmentList/' + $('#AssetID').val(), function (data) {
var items = '<option>Select a Segment</option>';
$.each(data, function (i, segment) {
items += "<option value='" + segment.Value + "'>" + segment.Text + "</option>";
});
$('#Segment').html(items);
});
});
$('#Segment').change(function () {
$.getJSON('/Restriction/SubAssetList/' + $('#Segment').val(), function (data) {
var items = '<option>Select a SubAsset</option>';
$.each(data, function (i, subAsset) {
items += "<option value='" + subAsset.Value + "'>" + subAsset.Text + "</option>";
});
$('#subAsset').html(items);
});
});
});
Here is my RestrictionController:
public class RestrictionController : Controller
{
private RestrictionContext db = new RestrictionContext();
//
// GET: /Restriction/
public ActionResult Index()
{
var restrictions = db.Restrictions.Include(r => r.Asset).Include(r => r.Segment).Include(r => r.SubAsset);
return View(restrictions.ToList());
}
//
// GET: /Restriction/Details/5
public ActionResult Details(int id = 0)
{
Restriction restriction = db.Restrictions.Find(id);
if (restriction == null)
{
return HttpNotFound();
}
return View(restriction);
}
//
// GET: /Restriction/Create
public ActionResult Create()
{
ViewBag.AssetID = new SelectList(db.Assets, "AssetID", "Asset_Name");
/*
ViewBag.SegmentID = new SelectList(db.Segments, "SegmentID", "Segment_Name");
ViewBag.SubAssetID = new SelectList(db.SubAssets, "SubAssetID", "SubAsset_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);
/*
ViewBag.SegmentID = new SelectList(db.Segments, "SegmentID", "Segment_Name", restriction.SegmentID);
ViewBag.SubAssetID = new SelectList(db.SubAssets, "SubAssetID", "SubAsset_Name", restriction.SubAssetID);
*/
return View(restriction);
}
//
// GET: /Restriction/Edit/5
public ActionResult Edit(int id = 0)
{
Restriction restriction = db.Restrictions.Find(id);
if (restriction == null)
{
return HttpNotFound();
}
ViewBag.AssetID = new SelectList(db.Assets, "AssetID", "Asset_Name", restriction.AssetID);
ViewBag.SegmentID = new SelectList(db.Segments, "SegmentID", "Segment_Name", restriction.SegmentID);
ViewBag.SubAssetID = new SelectList(db.SubAssets, "SubAssetID", "SubAsset_Name", restriction.SubAssetID);
return View(restriction);
}
//
// POST: /Restriction/Edit/5
[HttpPost]
public ActionResult Edit(Restriction restriction)
{
if (ModelState.IsValid)
{
db.Entry(restriction).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.AssetID = new SelectList(db.Assets, "AssetID", "Asset_Name", restriction.AssetID);
ViewBag.SegmentID = new SelectList(db.Segments, "SegmentID", "Segment_Name", restriction.SegmentID);
ViewBag.SubAssetID = new SelectList(db.SubAssets, "SubAssetID", "SubAsset_Name", restriction.SubAssetID);
return View(restriction);
}
//
// GET: /Restriction/Delete/5
public ActionResult Delete(int id = 0)
{
Restriction restriction = db.Restrictions.Find(id);
if (restriction == null)
{
return HttpNotFound();
}
return View(restriction);
}
//
// POST: /Restriction/Delete/5
[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(int id)
{
Restriction restriction = db.Restrictions.Find(id);
db.Restrictions.Remove(restriction);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
public JsonResult SegmentList(int Id)
{
var segment = from s in db.Segments
where s.AssetID == Id
select s;
return Json(new SelectList(segment.ToArray(), "SegmentID", "Segment_Name"), JsonRequestBehavior.AllowGet);
}
public JsonResult SubAssetList(int id)
{
var subAsset = from c in db.SubAssets
where c.SegmentID == id
select c;
return Json(new SelectList(subAsset.ToArray(), "SubAssetID", "SubAsset_Name"), JsonRequestBehavior.AllowGet);
}
public IList<Segment> Getsegment(int AssetID)
{
return db.Segments.Where(m => m.AssetID == AssetID).ToList();
}
[AcceptVerbs(HttpVerbs.Get)]
public JsonResult LoadClassesByAssetId(string Asset_Name)
{
var segmentList = this.Getsegment(Convert.ToInt32(Asset_Name));
var segmentData = segmentList.Select(m => new SelectListItem()
{
Text = m.Segment_Name,
Value = m.AssetID.ToString(),
});
return Json(segmentData, JsonRequestBehavior.AllowGet);
}
}
Thank you for your help!
Upvotes: 0
Views: 1541
Reputation:
Firstly, you don't have getters and setter on your model properties.
Secondly, your model has properties SegmentID
and subAssetID
but you have named the controls Segment
and subAsset
so they dont match your property names. You need to name them SegmentID
and SubAssetID
. I strongly recommend use use the strongly typed html helper methods to prevent this, for example @Html.DropDownListFor(m => m.AssetID, ViewBag.AssetID as SelectList, ""Select a Asset Class", null)
Upvotes: 1