background preloader

MVC

Facebook Twitter

Collection Binding in ASP.NET MVC3 with AJAX. There is a less-common scenario in web applications where we need to edit collection of objects and submit the whole back to the system.

Collection Binding in ASP.NET MVC3 with AJAX

For example, let us take the below view model: public class FruitModel... public string Name { get; set; } public bool IsFresh { get; set; } public bool IsPacked { get; set; } public decimal UnitPrice { get; set; } The UI for this scenario is shown below: Leave the top and bottom “Lorem ipsum” text, these are just gap fillers. The user can change the “IsFresh” and “IsPacked” settings of the fruits and the unit prices. Challenge This post addresses the following simple problems when using ASP.NET MVC3: Sending back collection of data to an MVC action Also send back additional parameter(s) to the same MVC action Sending back read-only data By Ajax Solution When the user hits this site, the HomeController’s Index will be called: public ActionResult Index()...

In the Index view, I’ve used NetAmount value of ViewBag as shown below: The partial view _Fruit is: 6 Tips for ASP.NET MVC Model Binding. Here are some tips on how to take advantage of model binding in your MVC projects.

6 Tips for ASP.NET MVC Model Binding

Tip #1: Prefer Binding Over Request.Form If you are writing your actions like this .. [AcceptVerbs(HttpVerbs.Post)] public ActionResult Create() { Recipe recipe = new Recipe(); recipe.Name = Request.Form["Name"]; // ... return View(); } .. then you are doing it all wrong. The model binder can save you from using the Request and HttpContext properties – those properties make the action harder to read and harder to test. Public ActionResult Create(FormCollection values) { Recipe recipe = new Recipe(); recipe.Name = values["Name"]; // ... return View(); } With the FormCollection you don’t have to dig into the Request object, and sometimes you need this low level of control. [AcceptVerbs(HttpVerbs.Post)] public ActionResult Create(Recipe newRecipe) { // ... return View(); } Tip #2: Custom model binders Model binding is also one of the extensibility points in the MVC framework.

In Conclusion. When to use ViewBag, ViewData, or TempData in ASP.NET MVC 3 applications-Rachel Appel on Software Development. "When should I use a ViewBag vs.

When to use ViewBag, ViewData, or TempData in ASP.NET MVC 3 applications-Rachel Appel on Software Development

ViewData vs. TempData objects? " -- a frequent question in online forums, during presentations, and at events. There are enough similarities and differences between these objects that warrant a closer look to see exactly how you can use each of these objects while developing MVC applications. All three objects are available as properties of both the view and controller. Incorporating dropdown lists of lookup data into an entity Components like a shopping cart Widgets like a user profile widget Small amounts of aggregate data While the TempData object works well in one basic scenario: Passing data between the current and next HTTP requests If you need to work with larger amounts of data, reporting data, create dashboards, or work with multiple disparate sources of data, you can use the more heavy duty ViewModel object.

ViewData & ViewBag objects ViewData ViewData is a dictionary object that you put data into, which then becomes available to the view. Dynamic V Strongly Typed Views - Ricka on MVC and related Web Technologies. There are three ways to pass information from a controller to a view in ASP.NET MVC 3: As a strongly typed model object.

Dynamic V Strongly Typed Views - Ricka on MVC and related Web Technologies

As a dynamic type (using @model dynamic) Using the ViewBag I’ve written a simple MVC 3 Top Blog application to compare and contrast dynamic and strongly typed views. The controller starts out with a simple list of blogs: Right-click in the IndexNotStonglyTyped() method and add a Razor view. Make sure the Create a strongly-typed view box is not checked. @{ ViewBag.Title = "IndexNotStonglyTyped"; } <h2>IndexNotStonglyTyped</h2> On the first line of the Views\Home\IndexNotStonglyTyped.cshtml file, add the model directive and the dynamic keyword.

@model dynamic Because we’re using a dynamic and not a strongly typed view, intellisense doesn’t help us.