Kevin Faustino
Kevin Faustino

Reputation: 143

What is the replacement of Controller.ReadFromRequest in ASP.NET MVC?

I am attempting to update a project from ASP.NET MVC Preview 3 to Preview 5 and it seems that Controller.ReadFromRequest(string key) has been removed from the Controller class. Does anyone know of any alternatives to retrieving information based on an identifier from a form?

Upvotes: 5

Views: 995

Answers (3)

matt
matt

Reputation: 1186

Looks like they've added controller.UpdateModel to address this issue, signature is:

UpdateModel(object model, string[] keys)

I haven't upgraded my app personally, so I'm not sure of the actual usage. I'll be interested to find out about this myself, as I'm using controller.ReadFromRequest as well.

Upvotes: 3

Iain Holder
Iain Holder

Reputation: 14262

could you redo that link in something like tinyurl.com?

I need this info too but can get that mega-link to work.

Upvotes: 0

Dane O'Connor
Dane O'Connor

Reputation: 77348

Not sure where it went. You could roll your own extension though:

public static class MyBindingExtensions {

public static T ReadFromRequest < T > (this Controller controller, string key) 
{
    // Setup
    HttpContextBase context = controller.ControllerContext.HttpContext;
    object val = null;
    T result = default(T);

    // Gaurd
    if (context == null)
        return result; // no point checking request

    // Bind value (check form then query string)
    if (context.Request.Form[key] != null)
        val = context.Request.Form[key];
    if (val == null) 
    {
        if (context.Request.QueryString[key] != null)
            val = context.Request.QueryString[key];
    }

    // Cast value
    if (val != null)
        result = (t)val;

    return result;
}

}

Upvotes: 2

Related Questions