HOW TO SEND DATA FROM VIEW TO CONTROLLER ACTION IN ASP.NET MVC?

Manoj Bhatt

Total Post:154

Points:1086
Posted by  Manoj Bhatt
 1817  View(s)
Ratings:
Rate this:

I developed a custom HtmlHelper extension method but that data is not posting Action.

 

HtmlHelper extension class:

 

public static class TestHtmlHelper

{

    public static MvcHtmlString CreateControl(this HtmlHelper helper, string tagName, IDictionary<string, string> attributes)

    {

        var newTag = new TagBuilder(tagName);

        newTag.MergeAttributes(attributes, true);

 

        return  MvcHtmlString.Create(newTag.ToString(TagRenderMode.Normal));

    }

 

    public static string Image(this HtmlHelper helper, string id, string url, string alternateText, object htmlAttributes)

    {

        // Create tag builder

        var builder = new TagBuilder("img");

 

        // Create valid id

        builder.GenerateId(id);

 

        // Add attributes

        builder.MergeAttribute("src", url);

        builder.MergeAttribute("alt", alternateText);

        builder.MergeAttributes(new RouteValueDictionary(htmlAttributes));

 

        // Render tag

        return builder.ToString(TagRenderMode.Normal);

    }

 

}

//View code

 

 @using (Html.BeginForm("Go","Home",FormMethod.Post))

{

    IDictionary<string, string> d = new Dictionary<string, string>();

    d.Add("type", "text");

    d.Add("id", "text1");

    d.Add("required", "required");

    @Html.Raw(Html.CreateControl("input", d))

    @Html.Raw(Html.Image("image1", "/Images/bullet.png", "bullet", new { border = "4px" }))

    d = null;

    d = new Dictionary<string, string>();

    d.Add("type", "submit");

    d.Add("value", "Go");

    @Html.Raw(Html.CreateControl("input", d))

    <span></span>

    d = null;

    d = new Dictionary<string, string>();

    d.Add("value", "text");

    d.Add("id", "span1");

    d.Add("text", "required");

    @Html.Raw(Html.CreateControl("span", d))

}

// Controller code

 

public ActionResult Index()

{

    ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";

 

    return View();

}

[HttpPost]

public ActionResult Go(string test)

{

    return Content(test);

}

I didn't get data in string test. Please help me for that.... I want to submit that data to DB.
  1. Ashish Pandey

    Post:128

    Points:898
    Re: How to send data from view to controller action in asp.net mvc?

    To get input values as parameters for an MVC action, you need to include NAME for the input types.

     

    I do not see NAME for any input types in your code.

    Also I do not see TEST in your code

    For example, if your form is -

     

    @using (Html.BeginForm("Submit","Ajax",FormMethod.Post))

    {

        <input type="text" name="Rami"/>

        <input type="submit" value="Go"/>

    }

      Modified On Apr-20-2016 01:56:12 AM
  1. Kamlakar Singh

    Post:194

    Points:1396
    Re: How to send data from view to controller action in asp.net mvc?

    Put your inputs inside a form tag. All the input data will be sent to the controller on form submit. Please see the example:

     

    View:

     

    @using (Html.BeginForm("Search", "Events"))

    {

       @Html.TextBox("name")

       <input type="submit" value="Search" />

    }

    Controller:

     

    public class EventsController: Controller

    {

       public ActionResult Search(string name)

       {

          //some operations goes here

     

          return View(); //return some view to the user

       }

    }

    If you need to work with more complex types just lern how to use models in ASP.NET MVC. Here is short example:

     

    Razor:

     

    @model UserModel

     

    @using (Html.BeginForm("Search", "Events"))

    {

       @Html.TextBoxFor(m => m.FirstName)

       @Html.TextBoxFor(m => m.LastName)

       <input type="submit" value="Search" />

    }

    Controller:

     

    public class EventsController: Controller

    {

       public ActionResult Search(UserModel model)

       {

          //some operations goes here

     

          return View(); //return some view to the user

       }

    }

    Model (C#):

     

    public class UserModel

    {

       public string FirstName { get; set; }

       public string LastName { get; set; }

    }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!