Suraj Shrestha
Suraj Shrestha

Reputation: 1808

Passing parameter from view to controller when button is clicked

I have created my own extension as:

public static MvcHtmlString hSearch(this HtmlHelper helper, string labelName, string labelCaption, string textName, string textValue, string tableName, string buttonId, 
        string actionName, string controllerName, object routeValues, object htmlAttributes)
    {            
        var textbuilder = new TagBuilder("input");
        textbuilder.MergeAttribute("id", textName);
        textbuilder.MergeAttribute("name", textName);
        textbuilder.MergeAttribute("value", textValue);
        textbuilder.MergeAttributes(new RouteValueDictionary(htmlAttributes));


        ModelMetadata metadata = ModelMetadata.FromStringExpression(labelName, helper.ViewData);
        String innerText = labelCaption ?? (metadata.DisplayName ?? (metadata.PropertyName ?? labelName.Split('.').Last())); 
        if (String.IsNullOrEmpty(innerText)) 
        { 
            return MvcHtmlString.Empty; 
        }
        TagBuilder labelbuilder = new TagBuilder("label");
        labelbuilder.Attributes.Add("for", TagBuilder.CreateSanitizedId(helper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(labelName)));
        labelbuilder.MergeAttributes(new RouteValueDictionary(htmlAttributes));
        labelbuilder.SetInnerText(innerText);            

        //return new MvcHtmlString(textbuilder.ToString());
        var buttonBuilder = new TagBuilder("button");
        buttonBuilder.MergeAttribute("id", buttonId);
        buttonBuilder.SetInnerText(buttonId);

        var formBuilder = new TagBuilder("form");
        var urlHelper = new UrlHelper(helper.ViewContext.RequestContext);

        formBuilder.Attributes.Add("action", urlHelper.Action(actionName, controllerName, routeValues));

        formBuilder.Attributes.Add("method", "Post");

        formBuilder.MergeAttributes(new RouteValueDictionary(htmlAttributes));

        formBuilder.InnerHtml = labelbuilder.ToString() + textbuilder.ToString() + buttonBuilder.ToString();
        return new MvcHtmlString(formBuilder.ToString());
    }

I used the extensions in view as:

 @Html.hSearch("lblSrch", "Company", "companyName", (string)TempData["cName"], "CHComp", "Search", "Fetch", "Home", null, null)

Now I want to pass tableName when I click the button to the controller.. my controller looks like this:

    public ActionResult Fetch(string search, string tablename)
    {
        var c = cbo.fetchData(search, tablename);
        return PartialView(c.ToList());
    }

Waiting for reply.. Thanks..

Upvotes: 0

Views: 1026

Answers (1)

StuartLC
StuartLC

Reputation: 107247

You haven't given us the code for your helper, but at a guess it writes out a label, a text field (textName), and a button. If this is the case, it will post / get companyName=someValue via HTTP back to your controller.

You would typically need to add a FormCollection to your controller if the fields are dynamically sent from the view. Alternatively, why not keep the name of the text search input static, e.g. name="search", which will bind to your controller's parameter of the same name.

Edit You can pass tableName back to the controller in a hidden field (<input type='hidden' name='tableName' value='{tableNameGoesHere}')

But as per above, your search string will have different names- the model binder isn't going to recognise it as string search.

Upvotes: 1

Related Questions