forum

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

How to send data from view to controller action in asp.net mvc?

Manoj Bhatt 3316 09-Dec-2014

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.

Updated on 10-Dec-2014

Can you answer this question?


Answer

2 Answers

Liked By