Passing data from controller to custom control view using ASP.NET MVC

I have a class like (OrderView.aspx) that shows the order details (account name, order date) as well as a list of order lines using a Repeater element. Each order line is represented by a custom control view (OrderLineView.ascx) that shows the order line details (item name, quantity, price). I have a model object called Order, which I use as the data source for all of this, which I pass as the model object for the view.

Each custom OrderLineView control has a Save and Delete button. I have a Save button posting form content in an OrderLine control using the Save method on the controller and then RedirectToAction back to the same order page (this updates the whole page, nothing about AJAXy about that). I have a Delete button referencing a method on the controller that is trying to delete and then RedirectToAction back to the same order page. However, if the delete fails, I want a small error message to appear next to the delete button when the page is rendered again (remember there is a delete button for every order line on the page, so I only need a message next to the one I clicked ). My questions:

1 - How do I pass this data from my controller method to a specific user control? Do I have to add it to the model somehow? Sounds like a bad idea (since it really isn't part of the model).

2 - Should I have an OrderLineController for OrderLine operations as well as an OrderController for Order operation? I just want to know if it's better to use a separate controller for each view.

3 - I've seen how some people can call RedirectToAction with an anonymous type like:

RedirectToAction("ViewOrder", new { Id = 1234, Message = "blabla"});

      

but this makes the Message value appear in the URL bar. I'm fine with that, but would prefer it not to show if possible.

4 - Also, to access the properties of the Model from the View, I do this all the time:

foo(((someModelType) this.ViewData.Model).SomeProperty);

      

I donโ€™t like this for a number of reasons, one of which is that I donโ€™t want my view to be associated with my model type (which is why I am using ViewPage instead of ViewPage). I would rather have a call like this:

foo(ModelEval("SomeProperty"));

      

Is there such a thing? I wrote mine, but I would like it if I didn't have to.

+1


source to share


2 answers


1

Check ModelState.

ViewData.ModelState.AddModelError("something.Name", "Please enter a valid Name");

      

ModelState is actually a dictionary, so you can identify errors based on control. I don't know if this is best practice, but it will probably work. Try something line by line

ViewData.ModelState.AddModelError("something#3.Name", "Please enter a valid Name");

      



and in your opinion you can put

<%= Html.ValidationMessage(string.format({"something{0}.Name", YourUniqueId))%>

      

4

You can type your opinion heavily, so you don't need to do that, but if you are worried about tight connection, it might turn you off. But having a strong type is no more closely related to the fact that the magic string points to this property of the model anyway. The former only gives you type safety and fame, which is intellisense.

+3


source


Since your OrderLine has a unique identifier, you can use it to create a key that will be placed in the ModelState error container.

public ActionResult Delete(int? Id)
{
    ModelState.AddModelError("OrderLine" + Id.Value, "Error deleting OrderLine# " + Id.Value);

    ...    
}

      

and then use the ValidatinoMessage helper. This will check the ModelState to see if the error exists, and if it does, it will display a message. Otherwise, it is empty.

<%= Html.ValidationMessage ("OrderLine" + Id)%>

      

The next version of the MVC Model will become a top level property, so the following



foo(((someModelType) this.ViewData.Model).SomeProperty);

      

can be written as

foo(Model.SomeProperty);

      

Model objects should already be printed unless you are using a public object as a property?

+1


source







All Articles