0

I have a View displaying a list of items. I want to display an existing item when user select one to "edit", or display a new item when user clicks "create". This "single item" window will be called in many different places so I want combine the function "Edit" and "Create" into one view, which is not too bad. I understand I can do this by creating a view page on the same level of the caller, and when user clicks "submit" on this item window, the item controller do its job and then redirect back to the caller page (item list or other caller). But I want to try another approach, which is make this Item window as a pop up window, when user clicks on the "show modal" button, the caller page makes an ajax call to controller, passing item's data (or its id) as parameter, and the controller generate the MvcHtmlString dynamically, return to the caller, and caller display the popup window using the MvcHtmlString received. Everything seems to working, but the massive html code in my controller looks pretty messy.

So I'm wonder how to use HtmlHelper to generate MvcHtmlString in controller, for example, I have a object type "item", how can I do things like Html.LabelFor(item => item.Name), or Html.EditorFor(item => item.Name) in controller and get the MvcHtmlString?

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
myajax95
  • 29
  • 7
  • 1
    Why is the controller creating the html? can't you just create a partial view (defined in your View folder) and have your controller to return the partial view? – Sparrow Feb 24 '17 at 22:09
  • If you want to do extraordinary things with your html, consider using partial views or display templates. – Joel Etherton Feb 24 '17 at 22:13
  • See [this question](http://stackoverflow.com/q/26362910/215552) and its answers for information on building HTML outside of the view. – Heretic Monkey Feb 24 '17 at 22:14
  • Yes. Partial view is an option. I'm thinking of using the pop in a more dynamic case. Assuming I'm editing a "face" on the view, and i need a pop up window to add an "eye". The information of this "eye" window is partially depends on the current "face" status, and partially using information from database. In this case partial view is not enough as it is relatively static. And I don't want to redirect back and forth as my "face" is not done yet. An windows style dynamic pop up will fit better. – myajax95 Feb 24 '17 at 22:40
  • Your popup can use a partial view – Sparrow Feb 24 '17 at 22:55

1 Answers1

0

You can extend your Htmlhelpers. If you want to just achieve edit/create button and you can change your viewmodel for condition or maybe you just want to use same model. You can use like this extension that i'm currently using on my views.

public static MvcHtmlString CustomActionLink(this HtmlHelper html, string action, string controller,
          string displayText, bool isCreate)
        {
            if (isCreate)
            {
                var targetUrl = UrlHelper.GenerateUrl("Default", action, controller,
                   null, RouteTable.Routes, html.ViewContext.RequestContext, false);
                var anchorBuilder = new TagBuilder("a");
                anchorBuilder.MergeAttribute("href", targetUrl);
                string classes = "btn btn-progress";
                anchorBuilder.MergeAttribute("class", classes);

                //Return as MVC string
                anchorBuilder.InnerHtml = displayText;
                return new MvcHtmlString(anchorBuilder.ToString(TagRenderMode.Normal));
            }
            else
            {
                var spanBuilder = new TagBuilder("span");
                spanBuilder.MergeAttribute("class", "btn btn-progress");
                spanBuilder.InnerHtml = displayText;
                return new MvcHtmlString(spanBuilder.ToString(TagRenderMode.Normal));
            }
        }
orhun.begendi
  • 937
  • 3
  • 16
  • 31