[英]mvc4 validation of partial view form
I have the following data model: 我有以下数据模型:
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; }
}
I have the following partial view called "CreatePartialView.cshtml" 我有以下名为“ 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>
and i have de follwing code 我有以下代码
$("#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);
});
In HomeController, we call InsertTraining() method of repository to save data in database. 在HomeController中,我们调用存储库的InsertTraining()方法将数据保存在数据库中。
[HttpPost]
public void Create(Training training)
{
_trainingRepository.InsertTraining(training);
} }
and before cilcking on submit-button i must validate the form of CreatePartialview.cshtml ,how can I do it? 在点击提交按钮之前,我必须验证CreatePartialview.cshtml的形式,我该怎么办?
Ok, there's a lot of things you haven't covered on your project or have not specified in your question. 好的,有很多事情您尚未在项目中涉及或未在问题中指定。 The answer would be too long and I would suggest to read a tutorial and then come back with any more specific questions. 答案将太长,我建议您阅读教程,然后再提出任何更具体的问题。 Read this 读这个
http://msdn.microsoft.com/en-us/library/ee256141(v=vs.100).aspx http://msdn.microsoft.com/zh-CN/library/ee256141(v=vs.100).aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.