Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted

1 solution

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.

C#
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):

C#
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);
}


Finally, the MultipleButtons view looks like this:

C#
@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!
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900