部分视图形式的mvc4验证

时间:2013-12-15 20:18:18

标签: c# jquery .net ajax asp.net-mvc-4

我有以下数据模型:

public class Training
{
    public Training()
    {
        this.ID = 0;
        this.Name = null;
        this.Instructor = null;
        this.StartDate = DateTime.Now;
        this.EndDate = DateTime.Now;
        this.Time = null;
        this.Duration = null;
    }

    public Training(int id, string name, string instructor, DateTime startdate, DateTime enddate, string time, string duration)
    {
        this.ID = id;
        this.Name = name;
        this.Instructor = instructor;
        this.StartDate = startdate;
        this.EndDate = enddate;
        this.Time = time;
        this.Duration = duration;
    }

    public Training(int id, string instructor)
    {
        this.ID = id;
        this.Instructor = instructor;
    }

    public int ID { get; set; }
    public string Name { get; set; }
    public string Instructor { get; set; }
    [DisplayFormat(DataFormatString = "{0:d MMM yyyy}")]
    public DateTime StartDate { get; set; }
    [DisplayFormat(DataFormatString = "{0:d MMM yyyy}")]
    public DateTime EndDate { get; set; }
    public string Time { get; set; }
    public string Duration { get; set; }
}

我有以下部分视图“CreatePartialView.cshtml”

<table>
<tr>
    <td></td>
    <td>Training Name</td>
    <td>
        <input type="text" id="trainingname" /></td>
</tr>
<tr>
    <td></td>
    <td>Instructor Name</td>
    <td>
        <select id="selectInstructor">
           <option selected="selected">Select Instructor</option>
        </select>
    </td>
</tr>
<tr>
    <td></td>
    <td>Select Time</td>
    <td>
        <input id="timepicker" />

    </td>
</tr>
<tr>
    <td></td>
    <td>Start Date</td>
    <td>
        <input id="startdatepicker" />
    </td>
</tr>
<tr>
    <td></td>
    <td>End Date</td>
    <td>
        <input id="enddatepicker" />
    </td>
</tr>
<tr>
    <td></td>
    <td>Duration</td>
    <td>
        <input type="text" id="duration" /></td>
</tr>
<tr>
    <td></td>
    <td>
        <input type="button" value="Submit" id="submit-button" class="btnStyleOne" />

    </td>
</tr>

我有以下代码

$("#submit-button").click(function () {
// On submit button click close dialog box
$("#createForm").dialog("close");

//Set inserted values
var name = $("#trainingname").val();
var selectInstructor = $("#selectInstructor").val();
var startdatepicker = $("#startdatepicker").val();
var enddatepicker = $("#enddatepicker").val();
var timepicker = $("#timepicker").val();
var duration = $("#duration").val();

// Call Create action method
$.post('/Home/Create', { "name": name, 
"instructor": selectInstructor, "startdate": startdatepicker, 
                         "enddate": enddatepicker, 
                         "time": timepicker, "duration": duration },
    function () {
        alert("data is posted successfully");
        window.location.reload(true);

     });

在HomeController中,我们调用存储库的InsertTraining()方法将数据保存在数据库中。

[HttpPost]
public void Create(Training training)
{
_trainingRepository.InsertTraining(training);

}

并且在提交按钮之前我必须验证CreatePartialview.cshtml的形式,我该怎么做?

1 个答案:

答案 0 :(得分:0)

好的,你项目中没有涉及的很多东西或者没有在你的问题中指明。答案太长了,我建议阅读一个教程,然后再回答任何更具体的问题。阅读本文

http://msdn.microsoft.com/en-us/library/ee256141(v=vs.100).aspx