验证razorview中的文本框 [英] Validate textbox in razorview

查看:124
本文介绍了验证razorview中的文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在部分页面上有一个文本框和两个按钮,单击这两个按钮后,我想验证文本框是否为空,我创建了两个表单,但不知道如何处理因为我需要对每个按钮执行不同的操作.

谢谢,
Rachana

I have one text box and two buttons on my partial page, on click of both button I wanted to validate textbox like it''s empty or not I created two form but I don''t know how to deal with it because I need to do different action for each button.

Thanks,
Rachana

推荐答案

您实际上并不需要多个表单,也可以使用单个表单实现相同的表单.与往常一样,有多种方法可以实现这一目标.我给他们之一.看看下面的例子...

添加一个名为UserData的类,该类将包含具有验证规则的属性.

You don''t really need to have multiple forms and achieve the same using a single form. As always, there are multiple ways to achieve this. I am giving one of them. Check out the following example...

Add a class called UserData that would contain a property with validation rules.

public class UserData
{
    [Required(ErrorMessage="Name is required")]
    public string Name { get; set; }
}



以下是操作方法(用于初始显示和后操作):



The following are the action methods (for initial display and post action):

public ActionResult MultipleButtons()
{
    return View();
}

[HttpPost]
public ActionResult MultipleButtons(UserData userData, string submit)
{
    // "submit" variable has the button clicked
    if (submit == "Action 1")
    {
         // carry out activities to be performed for 1st button click
    }
    else
    {
         // carry out activities to be performed for 2nd button click
    }

    return View(userData);
}



最后,MultipleButtons视图如下所示:



Finally, the MultipleButtons view looks like this:

@model TestMvcApp3.Controllers.UserData

@using (Html.BeginForm())
{
    @Html.TextBoxFor(m => m.Name) @* Displays a text box and binds it to the "Name" property *@
    @Html.ValidationMessageFor(m => m.Name) @* Display validation messages if any *@

    <input type="submit" value="Action 1" name="submit" /><input type="submit" value="Action 2" name="submit" />
}



希望这会有所帮助!



Hope this helps!


这篇关于验证razorview中的文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆