2013-12-15 76 views
0

我有以下數據模型:mvc4驗證

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> 

和i中的下面的部分視圖有德國代碼

$("#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); 

}

和cilcking上提交按鈕我必須驗證CreatePartialview.cshtml的形式之前,我該怎麼辦呢?

+1

您需要創建與驗證屬性的強類型視圖模型的局部視圖。這樣你也可以使用標準的Razor html助手('@ Html.TextBoxFor(m => m.Name)'),並且你可以添加服務器端/客戶端驗證到這個模型和驗證消息到你的視圖。 – bump

+0

謝謝你的ansower,但在這裏我不知道如何驗證表單,而不使用強類型的viewmodel,阿拉那裏有其他解決方案,我的情況下,我需要更多解釋:) – user3104968

+0

+1謝謝你,幫助我, – user3104968

回答