0

誰かが mvc3 html 編集可能グリッドで検証を行うのを手伝ってくれますか? 列の値の合計が 100 を超えてはなりません。jQuery を使用して検証したり、サーバー側の検証を行うことはできますか?

4

2 に答える 2

0

これはサーバー側の検証です

クラス:

public class Party
{
[Required(ErrorMessage = "Start date is required")]
public DateTime StartDate { get; set; }

[Required(ErrorMessage = "Duration is required")]    
public int DurationInHours { get; set; }

[Required(ErrorMessage = "No. of joinees is required")]
[Range(2, 10, ErrorMessage = "No. of joinees should be minimum 2 and not more than 10")]
public int NoOfJoinees { get; set; }    

public bool Drinks { get; set; }
}

コントローラ:

public class PartyController: Controller
{
public ActionResult Index()
{
    return View();
}
} 

意見:

@model CustomValidation.MVC.Models.Party

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


Start date (MM/dd/yyyy HH:mm:ss AM/PM) *: @Html.TextBoxFor(x => x.StartDate, new { size = 25 })
Duration (Hours) *: @Html.DropDownListFor(x => x.DurationInHours, new[]{
                        new SelectListItem(){ Text = "1", Value = "1"},
                        new SelectListItem(){ Text = "2", Value = "2"},
                        new SelectListItem(){ Text = "3", Value = "3"},
                        new SelectListItem(){ Text = "4", Value = "4"},
                        new SelectListItem(){ Text = "5", Value = "5"}
                        }, "Select the duration", new { style = "width:180px" })


    No. of joinees *: @Html.TextBoxFor(x => x.NoOfJoinees, new { size = 5 })

    Drinks? @Html.CheckBoxFor(x => x.Drinks)

    <input type="submit" value="Host the party!" />
}

クライアント側の検証:

HTML

<input type="text" id="UserName" name="UserName"/>
<input type="button" onclick="Validation()" value="Enter" />

Javascript:

function Validation() {
var data= {
UserName: $('#UserName').val()
};

if (data.UserName.trim() == "" || data.UserName== undefined) {
$("#ShowWarning").html('<img src="/Image/warning.jpg" title="Please Enter UserName!">').show();
}

また、以下の例も確認できます

http://www.mindstick.com/Articles/d17c1dc9-e00b-4c13-94e7-87dacdca027f/?Validation%20in%20ASP%20NET%20MVC3

私はあなたを助けることを願っています

于 2013-09-23T19:05:36.303 に答える