Click here to Skip to main content
15,917,538 members
Please Sign up or sign in to vote.
1.24/5 (3 votes)
See more:
Dear friends,

I am creating a application in MVC ,where in "VIEW" I have two asp:TextBox and a asp:Button. now, I want that when I click on this button the Textboxes values should passed to "controller".i.e, there will be a click event where values will be passed and saved to database.


I don't know how click event works in MVC.

Please friends help me .

Thanks in advance
Posted
Updated 26-Feb-17 3:30am
Comments
deepakaitr12345 8-Sep-12 3:21am    
Create the class
--------------
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class MultiButtonInfo : ActionNameSelectorAttribute
{
public string MatchFormKey { get; set; }
public string MatchFormValue { get; set; }
public override bool IsValidName(ControllerContext controllerContext, string actionName, MethodInfo methodInfo)
{
return controllerContext.HttpContext.Request[MatchFormKey] != null &&
controllerContext.HttpContext.Request[MatchFormKey] == MatchFormValue;
}
}
----------------------------
then pass the model or the para in this way
where MatchFormKey is the name of your button and MatchFormValue is the value of the button

[HttpPost]
[MultiButtonInfo(MatchFormKey = "BtnReset", MatchFormValue = "Reset")]
[ValidateInput(false)]
public ActionResult ResetFilter(formCollection fc)
{
}

using the form collection you will get the control's id and value

Hope it will help you..
Thanks

1 solution

MVC is not compatible with ASP.NET Web Control.
We can only use HTML Control to support the flow of the MODEL - VIEW - CONTROLLER.

Your Solution :

In the View Part:

@using (Html.BeginForm("GetValue","Home"))
{

<input id="Text1" type="text" name="txtOne"/>
<input id="Button1" type="submit" value="button" />
}


In the Controller "Home" == > Action "GetValue" .. You get the TextBox Value

SQL
[HttpPost]
       public ActionResult GetValue()
       {
           string textboxValue = Request.Form["txtOne"];
           //Code To Insert into the Database
           return View();
       }



Here [HttpPost] attribute is required because We Posting the data.. to Another View.
 
Share this answer
 
Comments
Member 9437509 22-Sep-12 6:03am    
i got it

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