0

Been working on creating an interface to allow a modular approach to the UI, the background:

Allows users to drag and drop a module onto a div jQUery posts back to controller with the module and panel names Controller returns a JsonResult containing a view that has been rendered, specific to that module

Here is a picture of the UI so you can sort of see what I am doing:

Image

Now, what I am trying to do, is in that JsonResult (Which contains a string output of a view rendering), is save some data back to the model, and refresh that dynamically rendered view, so that just the panel (Where the view has been rendered) updates.

Sounds complicated i know, so here is some code:

 [AcceptVerbs(HttpVerbs.Post)]
    public JsonResult AddModule(string id, string returnTo)
    {
        string content = RenderView(id);
        return Json(new { Target = returnTo, Content = content });
    }
    private string RenderView(string moduleName)
    {
        string result = "";

        ContentModule module = (ContentModule)Activator.CreateInstance(Type.GetType("TrustMRM.BLL.ContentModules." + moduleName + ",TrustMRM.BLL"));
        module.TrustID = Settings.Default.TrustID;
        module.DataBind();
        this.ViewData.Model = module;

        using (var sw = new System.IO.StringWriter())
        {
            ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(this.ControllerContext, moduleName);
            var viewContext = new ViewContext(this.ControllerContext, viewResult.View, this.ViewData, this.TempData, sw);
            viewResult.View.Render(viewContext, sw);
            result = sw.GetStringBuilder().ToString();
        }
        return result;
    }

The above is what handles the 'drop' of the module. I have an abstract class, ContentModule, and an implementation called BLLForumModule, there is a matching view, BLLForumModule.cshtml, that gets built, and returned in that string, strongly bound tot he BLLForumModule.

What is rendered is a drop down list, equal to some data to configure that particular module:

@model TrustMRM.BLL.ContentModules.BLLForumModule
@{

Layout = null;
}

@if (Model.IsConfigured)
{ 
    <span>I am configured</span>
}
else
{
using (Html.BeginForm("RefreshModule", "Home"))
{
    <h3 class="panelHeader">@Html.DisplayTextFor(m => m.Title)</h3>
    <span>Select group</span>
    @Html.DropDownListFor(m => m.SelectedGroupID, Model.GroupSelection.Select(t => new SelectListItem { Text = t.GroupName, Value = t.GroupID.Value.ToString() }));
    @Html.HiddenFor(x => x.ModuleID);                                                                                                                     
    <input type="submit" value="Ok" />                                                                                                                                                           

    }
}

Now, I am unsure of what to return, or how to handle this post in order to refresh that view, the one that was rendered as a string and sent back, any insight into this, and if anyone has done something similar before, perhaps my rendering the view to a string is the wrong approach?

The code to accept the form post:

public ActionResult RefreshModule(string ModuleID)
    {
        return View();
    }

(Doesn't work)

tereško
  • 58,060
  • 25
  • 98
  • 150
Daniel Dawes
  • 975
  • 5
  • 16

1 Answers1

1

Something like that will help you Using Ajax.BeginForm with ASP.NET MVC 3 Razor

Just use Ajax.BeginForm and provide an id of replaced element.

Attach validation after ajax request here MVC3 Unobtrusive Validation Not Working after Ajax Call

Community
  • 1
  • 1
Vladmir
  • 1,255
  • 9
  • 13
  • Thanks for this, seems to be pretty much the direction I was thinking to head, really appreciate it, nice to see the other entry has a couple of approaches too – Daniel Dawes Mar 07 '13 at 13:30