views:

3169

answers:

4

Hi im new to MVC and I've fished around with no luck on how to build MVC User Controls that have ViewData returned to them. I was hoping someone would post a step by step solution on how to approach this problem. If you could make your solution very detailed that would help out greatly.

Sorry for being so discrete with my question, I would just like to clarify that what Im ultimatly trying to do is pass an id to a controller actionresult method and wanting to render it to a user control directly from the controller itself. Im unsure on how to begin with this approach and wondering if this is even possible. It will essentially in my mind look like this

public ActionResult RTest(int id){
RTestDataContext db = new RTestDataContext();
var table = db.GetTable<tRTest>();
var record = table.SingleOrDefault(m=> m.id = id);

return View("RTest", record);
}

and in my User Control I would like to render the objects of that record and thats my issue.

A: 

I am pretty sure view data is accessible inside user controls so long as you extend System.Web.Mvc.ViewUserControl and pass it in. I have a snippet of code:

<%Html.RenderPartial("~/UserControls/CategoryChooser.ascx", ViewData);%>

and from within my CategoryChooser ViewData is accessible.

stimms
+8  A: 

If I understand your question, you are trying to pass ViewData into the user control. A user control is essentially a partial view, so you would do this:

<% Html.RenderPartial("someUserControl.ascx", viewData); %>

Now in your usercontrol, ViewData will be whatever you passed in...

Ricky
You mean <% Html.RenderPartial("someUserControl", viewData); %> if you put your file on Shared folder
Junior Mayhé
A: 

Not sure if I understand your problem completely, but here's my answer to "How to add a User Control to your ASP.NET MVC Project".

In Visual Studio 2008, you can choose Add Item. In the categories at the left side, you can choose Visual C# > Web > MVC. There's an option MVC View User Control. Select it, choose a name, select the desired master page and you're good to go.

Casper
+1  A: 

OK here it goes -- We use Json data

In the aspx page we have an ajax call that calls the controller. Look up the available option parameters for ajax calls.

url: This calls the function in the class.(obviously) Our class name is JobController, function name is updateJob and it takes no parameters. The url drops the controllerPortion from the classname. For example to call the updateJob function the url would be '/Job/UpdateJob/'.

var data = {x:1, y:2};
$.ajax({
data: data,
cache: false,
url: '/ClassName/functionName/parameter',
dataType: "json",
type: "post",
success: function(result) {
//do something
},
error: function(errorData) {
alert(errorData.responseText);
}
}
);

In the JobController Class:

public ActionResult UpdateJob(string id)
{
   string x_Value_from_ajax = Request.Form["x"];
   string y_Value_from_ajax = Request.Form["y"];
   return Json(dataContextClass.UpdateJob(x_Value_from_ajax, y_Value_from_ajax));
}

We have a Global.asax.cs page that maps the ajax calls.

public class GlobalApplication : System.Web.HttpApplication
    {
     public static void RegisterRoutes(RouteCollection routes)
     {
      routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("Default",                 // Route name
    "{controller}/{action}/{id}",     // URL with parameters
        new { controller = "EnterTime", action = "Index", id = "" }  // Parameter defaults  (EnterTime is our default controller class, index is our default function and it takes no parameters.)
    );
  }
}

I hope this gets you off to a good start. Good luck

Brad8118