我是否需要自定义ViewMode(MVVM)?

时间:2015-08-26 11:42:20

标签: c# asp.net asp.net-mvc entity-framework

我是ASP.Net MVC的新手

我想显示一个不同的视图来选择学校和Challan,然后插入所选Challan的ID和所有学生(所选学校的)的ID,以便插入Joinning表中,即。 ChallanAndStudents表

我有以下实体

tbl.FeeChallan, 

tbl.Students (Students belong to different schools),

tbl.ChallanAndStudents (the joining Table of tbl.FeeChallan and tbl.Students )

tbl.Schools 

加入表格有StudentIDFeeChallanID

正如业务逻辑所说:FeeChallans可以来自单个学生或来自整个学校。

如果选择全校,我会允许用户从DropDownList中选择School,从第二个DropDownList中选择ChallanNumber。

然后在提交时,我必须插入ChallanFormIDStudentID(所选学校的所有学生加入加入表)

VIEWMODE:

public class vmAssignChallanToWholeSchool
{
    public int ChallanFormID { get; set; }
    public int SchoolID { get; set; }   
}

控制器:

public ActionResult AssignChallanToSchool()
    {
        vmAssignChallanToWholeSchool ChallanToSchool = new vmAssignChallanToWholeSchool();



    ViewBag.ChallanForm = new SelectList(db.ChallanForm.ToList(), "ChallanFormID", "ChallanFormNumber", ChallanToSchool.ChallanFormID);
    ViewBag.Schools = new SelectList(db.Schools, "ID", "Name", ChallanToSchool.SchoolID);

    return View();


}

查看:

@model EBS_MVC.ViewModels.vmAssignChallanToWholeSchool
@{
    ViewBag.Title = "AssignChallanToSchool";
}

<h2>AssignChallanToSchool</h2>


<div class="form-group">
    @Html.LabelFor(model => model.ChallanFormID, "ChallanID", htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.DropDownList( "ChallanForm", new SelectList(ViewBag.ChallanForm))
        @Html.ValidationMessageFor(model => model.ChallanFormID, "", new { @class = "text-danger" })
    </div>
</div>

并且我是否需要一个自定义ViewModel来完成这项工作,就像上面提到的那样?

命中和尝试列表:

  

EntityType:EntitySet'vmAssignChallanToWholeSchools'基于类型   'vmAssignChallanToWholeSchool'没有定义键。   在此处输入图像描述

     

System.Web.HttpException:DataBinding:   'System.Web.Mvc.SelectListItem'不包含带有的属性   名字'ChallanFormID'。

1 个答案:

答案 0 :(得分:0)

如果我理解正确,那么Challan就是有人参加学校的时候。

因此,如果您在ChallanController中执行所有操作,我认为整个设置会更容易。然后,一个简单的Add将在正确的控制器中执行您想要的操作。