Home > DeveloperSection > Forums > How to send data from view to controller action in asp.net mvc?
Manoj Bhatt
Manoj Bhatt

Total Post:153

Points:1079
Posted on    December-09-2014 11:48 PM

 ASP.Net ASP.NET MVC  Controller 
Ratings:


 2 Reply(s)
 1294  View(s)
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.


Ashish Pandey

Total Post:128

Points:898
Posted on    December-10-2014 5:45 AM

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

Kamlakar Singh
Kamlakar Singh

Total Post:194

Points:1396
Posted on    December-10-2014 6:42 AM

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; }

}


Don't want to miss updates? Please click the below button!

Follow MindStick