asp.net razor web pages validation and using multi

2019-07-14 07:20发布

i wanted to use two froms in my razor web pages(not mvc) and my problem is working with more than one form how do i say to my project that which form button is clicked and another problem is validation because in web form project i was using validation group to separate validations but here i do not know how to deal with it both in client side validate and server side validation here is my codes :

@{
Page.Title = "";
Layout = "~/_layout.cshtml";

    Validation.Add("txt1", Validator.Required("can not leave empty"));
    Validation.Add("txt2", Validator.Required("can not leave empty"));


if (IsPost)
{

    if (!Validation.IsValid())
    {
    Validation.AddFormError("there are errors");
    }


}


}
    <style>
    .validation-summary-errors {
            border: 2px solid #990099;
            color: red;
        }

        .field-validation-error {
            color: Red;
        }

        .input-validation-error {
            color: #990099;
            background-color: #ff80ff;
            border-top: 2px solid #990099;
            border-left: 2px solid #990099;
        }
    </style>
    <div>
    <script src="~/jquery-1.7.2.min.js"></script>
    <script src="~/jquery.validate.min.js"></script>
    <script src="~/jquery.validate.unobtrusive.min.js"></script>
    </div>


 <p style="height:10px;"></p>

 <div>
<form name="f1" action="/" method="post">
<div>
    <input name="txt1" type="text" 
        class="@Validation.ClassFor("txt1")"
         />

    @Html.ValidationMessage("txt1")
</div>
<div>
    <input type="submit" name="s1" value="Send 1" />
</div>
</form>

</div>
<br />

<form name="f2" action="/" method="post">
<div>
    <input name="txt2" type="text" 
        class="@Validation.ClassFor("txt2")"
        />

    @Html.ValidationMessage("txt2")
</div>
<div>
    <input type="submit" name="s2" value="Send 2" />
</div>

 </form>

标签: razor
2条回答
狗以群分
2楼-- · 2019-07-14 07:51

You can determine which form was submitted by checking to see which submit button was clicked. Its name and value will be in the Request.Form collection. If the first form in your example code was submitted, Request["s1"] would have the value "Send 1". Request["s2"].IsEmpty() would be true.

The easiest way to get round the validation issue is to use the same name ("txt1") for both text boxes. Otherwise you will have to leave the Validation helpers and write your own conditional validation routines (client and server) based on which form is submitted.

查看更多
手持菜刀,她持情操
3楼-- · 2019-07-14 07:57

Check out this link for handling multiple form requests http://forums.asp.net/t/1678062.aspx/1

You can handle validation in each sub section. Hope that helps

查看更多
登录 后发表回答