0

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. I want to submit that data to DB.

2
  • 1
    I do not see any input type with name = test in your form. To get the value on the action parameters, those inputs must have same name. Commented Feb 10, 2014 at 12:07
  • 2
    Check this stackoverflow.com/questions/18873098/… Commented Feb 10, 2014 at 12:10

2 Answers 2

3

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"/>
}

Output ScreenShot -

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

This is a weird screenshot. I thought they have the VS Debugger inside of SO :-)
@MalcolmFrexner, hahaha :-) . Updated with a heading.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.