Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将dropdownlist的选定值作为asp.net mvc模型的属性传递_C#_Asp.net Mvc_Entity Framework - Fatal编程技术网

C# 将dropdownlist的选定值作为asp.net mvc模型的属性传递

C# 将dropdownlist的选定值作为asp.net mvc模型的属性传递,c#,asp.net-mvc,entity-framework,C#,Asp.net Mvc,Entity Framework,我正在尝试使用dropdownlist获取模型的三个属性的选定值,dropdownlist使用脚本填充下一个dropdownlist 所以我的问题是我用以下代码替换EF代码: @Html.DropDownList("AssetID", ViewBag.AssetID as SelectList, "Select a Asset Class", new { id="AssetID" })<br /> <select id="Segment" name="s

我正在尝试使用dropdownlist获取模型的三个属性的选定值,dropdownlist使用脚本填充下一个dropdownlist

所以我的问题是我用以下代码替换EF代码:

@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 />
这是我的模型限制的三个属性(外键)。 我的问题是,现在modelState无效,因此限制的引用没有添加到数据库中,可能我不得不DropDownlistFor将所选值绑定到属性,但我不知道如何绑定。此外,如果需要,我可以发布脚本

我的型号限制:

    public string portefeuille
    public int AssetID
    public int SegmentID
    public int SubAssetID
    public int Min
    public int Max
基于上一个选定项目填充下拉列表的我的脚本:

  $(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);
    });
});
$(函数(){
$('#AssetID')。更改(函数(){
$.getJSON('/Restriction/SegmentList/'+$('#AssetID').val(),函数(数据){
var items='选择一个段';
$。每个(数据、功能(i、段){
项目+=“”+段。文本+“”;
});
$('#段').html(项目);
});
});
$('#段')。更改(函数(){
$.getJSON('/Restriction/SubAssetList/'+$('#Segment').val(),函数(数据){
变量项='选择子部件';
$。每个(数据、功能(i、子组件){
项目+=“”+子组件文本+“”;
});
$('子组件').html(项目);
});
});
}))

这是我的限制控制器:

  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);
    }
}
公共类限制控制器:控制器
{
private RestrictionContext db=新的RestrictionContext();
//
//获取/限制/
公共行动结果索引()
{
var restrictions=db.restrictions.Include(r=>r.Asset)。Include(r=>r.Segment)。Include(r=>r.SubAsset);
返回视图(restrictions.ToList());
}
//
//获取:/Restriction/Details/5
公共操作结果详细信息(int id=0)
{
限制=db.Restrictions.Find(id);
if(限制==null)
{
返回HttpNotFound();
}
返回视图(限制);
}
//
//获取/限制/创建
公共操作结果创建()
{
ViewBag.AssetID=新的选择列表(db.Assets,“AssetID”,“Asset_Name”);
/*
ViewBag.SegmentID=新的选择列表(db.Segments,“SegmentID”,“Segments_Name”);
ViewBag.SubAssetID=新选择列表(db.SubAssets,“SubAssetID”,“subassetu Name”);
* */
返回视图();
}
//
//POST:/Restriction/Create
[HttpPost]
公共操作结果创建(限制)
{
if(ModelState.IsValid)
{
db.Restrictions.Add(限制);
db.SaveChanges();
返回操作(“索引”);
}
ViewBag.AssetID=新选择列表(db.Assets,“AssetID”,“资产名称”,restriction.AssetID);
/*
ViewBag.SegmentID=新的选择列表(db.Segments,“SegmentID”,“Segments\u Name”,restriction.SegmentID);
ViewBag.SubAssetID=新选择列表(db.SubAssets,“SubAssetID”,“subassetu Name”,restriction.SubAssetID);
*/
返回视图(限制);
}
//
//获取:/Restriction/Edit/5
公共操作结果编辑(int id=0)
{
限制=db.Restrictions.Find(id);
if(限制==null)
{
返回HttpNotFound();
}
ViewBag.AssetID=新选择列表(db.Assets,“AssetID”,“资产名称”,restriction.AssetID);
ViewBag.SegmentID=新的选择列表(db.Segments,“SegmentID”,“Segments\u Name”,restriction.SegmentID);
ViewBag.SubAssetID=新选择列表(db.SubAssets,“SubAssetID”,“subassetu Name”,restriction.SubAssetID);
返回视图(限制);
}
//
//发布:/Restriction/Edit/5
[HttpPost]
公共操作结果编辑(限制)
{
if(ModelState.IsValid)
{
db.Entry(restriction).State=EntityState.Modified;
db.SaveChanges();
返回操作(“索引”);
}
ViewBag.AssetID=新选择列表(db.Assets,“AssetID”,“资产名称”,restriction.AssetID);
ViewBag.SegmentID=新的选择列表(db.Segments,“SegmentID”,“Segments\u Name”,restriction.SegmentID);
ViewBag.SubAssetID=新选择列表(db.SubAssets,“SubAssetID”,“subassetu Name”,restriction.SubAssetID);
返回视图(限制);
}
//
//获取:/Restriction/Delete/5
公共操作结果删除(int id=0)
{
限制=db.Restrictions.Find(id);
if(限制==null)
{
返回HttpNotFound();
}
返回视图(限制);
}
//
//POST:/Restriction/Delete/5
[HttpPost,ActionName(“删除”)]
公共行动结果删除已确认(内部id)
{
限制=db.Restrictions.Find(id);
db.限制。删除(限制);
db.SaveChanges();
返回操作(“索引”);
}
受保护的覆盖无效处置(布尔处置)
{
db.Dispose();
基地。处置(处置);
}
公共JsonResult段列表(int Id)
{
var段=以分贝段表示的s
其中s.AssetID==Id
选择s;
返回Json(新的SelectList(segment.ToArray(),“SegmentID”,“segment_Name”),JsonRequestBehavior.AllowGet);
}
公共JsonResult子组件列表(int id)
{
var subAsset=从db.subAsset中的c开始
其中c.SegmentID==id
选择c;
返回Json(新的SelectList(subAsset.ToArray(),“SubAssetID”,“subAsset_Name”),JsonRequestBehavior.AllowGet);
}
公共IList Getsegment(int AssetID)
{
返回db.Segments.Where(m=>m.AssetID==AssetID.ToList();
}
[接受动词(HttpVerbs.Get)]
public JsonResult LoadClassesByAssetId(字符串资产名称)
{
var segmentList=this.Getsegment(Convert.ToInt32(资产名称));
分段变量
  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);
    }
}