【问题标题】:Multiple tables data input form mvc多表数据输入表单mvc
【发布时间】:2018-01-03 20:09:53
【问题描述】:

我正处于我的 asp.net 项目之间,并且被困在非常基本的事情上,但遗憾的是无法从在线博客中获得太多帮助。

我有 4 个表 Student、Course、Department、Applied。我正在设计一个表格供学生在线申请课程。

学生将填写他的所有个人详细信息,例如姓名、联系方式等 之后,学生将从下拉列表中选择部门。根据部门的选择,专业课程将显示在课程下拉列表中。

我无法保存在此表单中输入的数据。我对这些下拉列表做错了。当我从下拉列表中选择部门时,填写学生个人详细信息后,所有个人详细信息都被删除了。以下是我的代码。

public class Student
{
    public int StudentID { get; set; }        
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime DateOfBirth { get; set; }
    public string Address { get; set; }
    public double PhoneNumber { get; set; }
    public string Email { get; set; }
    public DateTime AppliedDate { get; set; }
    public virtual ICollection<Applied> Applications { get; set; }
}

public class Course
{
    public int CourseID { get; set; }       //PK

    public int DepartmentID { get; set; }   //FK
    public string CourseName { get; set; }
    public string CoursCode { get; set; }
    public int NfqLevel { get; set; }
    public int CreditHrs { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime FinishDate { get; set; }

    public virtual Department Department { get; set; }
    public virtual ICollection<Applied> Applications { get; set; }       
}

public class Department
{
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int DepartmentID { get; set; }       //PK
    public string DpName { get; set; }
    public virtual ICollection<Course> Courses { get; set; }
}

public enum Status { Applied, Approved, Rejected }

public class Applied
{
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int AppliedID { get; set; }
    public int StudentID { get; set; }      //FK
    public int CourseID { get; set; }       //FK

    public Status? ApplicationStatus { get; set; }

    public virtual Student Student { get; set; }
    public virtual Course Course { get; set; }
    public virtual Enrolment Enrolment { get; set; }
}

public class Enrolment
{
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int EnrolmentID { get; set; }        //PK

    public int AppliedID { get; set; }          //FK
    public int TotalFee { get; set; }
    public int FeeReceived { get; set; }
    public int BalFee { get; set; }

    public virtual Applied Application { get; set; }

}

ViewModel 类:

public class Application2
{
    public int StudentID { get; set; }       

    public string FirstName { get; set; }

    public string LastName { get; set; }

    [DataType(DataType.Date)]
    public DateTime DateOfBirth { get; set; }

    public string Address { get; set; }

    [DataType(DataType.PhoneNumber)]
    public double PhoneNumber { get; set; }

    [DataType(DataType.EmailAddress)]
    public string Email { get; set; }

    [DataType(DataType.Date)]
    public DateTime AppliedDate { get; set; }

    public int AppliedID { get; set; }

    public int CourseID { get; set; }       

    public string CourseName { get; set; }

    public string DpName { get; set; }
}



public class ApplicationController : Controller
{
    private RegistrarsContext db = new RegistrarsContext();

    //GET: StudentApplication
    public ActionResult Index(int? SelectedDepartment, int? SelectedCourse)
    {

        {
            var departments = db.Departments.OrderBy(q => q.DpName).ToList();

            ViewBag.SelectedDepartment = new SelectList(departments, "DepartmentID", "DpName", SelectedDepartment); 


            int departmentID = SelectedDepartment.GetValueOrDefault();

            IQueryable<Course> courses = db.Courses
                .Where(c => !SelectedDepartment.HasValue || c.DepartmentID == departmentID)
                .OrderBy(d => d.CourseID)
                .Include(d => d.Department);

            ViewBag.SelectedCourse = new SelectList(courses, "CourseID", "CourseName");

        }
        return View();
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(Application2 application)
    {
       var student = new Student {StudentID=application.StudentID, FirstName=application.FirstName, LastName=application.LastName, DateOfBirth=application.DateOfBirth, Address=application.Address, PhoneNumber=application.PhoneNumber, Email=application.Email };

            var apply = new Applied {AppliedID=application.AppliedID, CourseID=application.CourseID, StudentID=application.StudentID  };

        using (var db = new RegistrarsContext())
        {
            db.Students.Add(student);
            db.Applied.Add(apply);
            db.SaveChanges();
            return View("Index");
        }

    }

查看编码:

@model StudentsRegistration.ViewModels.Application

@{
    ViewBag.Title = "Student Appication";
}

<h2>Student Application Form</h2>


@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Student Details </h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })


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

        <div class="form-group">
            @Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.FirstName)
                @Html.ValidationMessageFor(model => model.StudentID)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.LastName)
                @Html.ValidationMessageFor(model => model.LastName)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.DateOfBirth, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.DateOfBirth)
                @Html.ValidationMessageFor(model => model.DateOfBirth)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Address, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Address)
                @Html.ValidationMessageFor(model => model.Address)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.PhoneNumber, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.PhoneNumber)
                @Html.ValidationMessageFor(model => model.PhoneNumber)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Email)
                @Html.ValidationMessageFor(model => model.Email)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.AppliedDate, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.AppliedDate)
                @Html.ValidationMessageFor(model => model.AppliedDate)
            </div>
        </div>
        <br>
        <br>

        <div class="form-group" ,>
            Select Department:


            @Html.DropDownList("SelectedDepartment", "Select Department")
            <input type="submit" value="Select" />

        </div>

        <div class="form-group">

            Select Course:

            @Html.DropDownList("SelectedCourse", "Select Course")

        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Submit" class="btn btn-default" />
            </div>
        </div>

    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

【问题讨论】:

  • 该错误更有可能出现在 View 的 razor/HTML 代码中,因此如果您可以发布该错误会有所帮助
  • 查看代码也贴出来了
  • 您应该编辑问题,而不是将其作为答案发布 :-) 但我会看看

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


【解决方案1】:

&lt;input type="submit" value="Select" /&gt; 是我认为的问题。我假设您在选择部门时按此?这将提交整个表单并保存数据。然后它会将您返回到同一视图的空白版本。

您需要先摆脱按钮。然后你需要一些javascript来处理你的部门下拉列表中的“更改”事件。然后,该代码将向服务器发出 ajax 请求,并传入所选部门的 ID。在服务器上,您定义了一个新的操作方法,该方法接受一个部门 ID,并返回一个 JsonResult,其中包含与部门关联的课程列表。它通过 ajax 调用返回到页面,javascript 读取它并用结果重新填充 Courses 下拉列表。

如果您需要更多信息,此答案包含一个很好的教程。 Easiest way to create a cascade dropdown in ASP.NET MVC 3 with C# 。这里还有很多其他人:https://www.google.co.uk/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#safe=active&q=mvc+cascading+dropdownlist

【讨论】:

  • 非常感谢您指出有问题的部分。这些链接很有帮助。
  • 没问题。如果答案对您有所帮助,请投票和/或标记为已接受的答案 - 谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-07
  • 2015-11-25
  • 1970-01-01
  • 1970-01-01
  • 2014-06-14
  • 1970-01-01
相关资源
最近更新 更多