ValueProvider no llamado para propiedad que no está en forma

I'm not having any luck getting a custom value provider to be called for a property on my view model. I'm trying to encapsulate some of the standard request data, such as UserHostAddress into my view model, using a custom value provider.

For example, my view model looks like:

public class MyViewModel
{
    public string Name { get; set; }

    public string IPAddress { get; set; }
}

I want my custom value provider to populate the IPAddress property automatically. Unfortunately, GetValue is never called for that property. This seems strange, as I thought the DefaultModelBinder would iterate over the model's properties and check the value providers for a value.

preguntado el 24 de agosto de 12 a las 04:08

Did you register your custom value provider factory? -

I think I have the same problem. GetValue , solamente seems to get called for the to-be-populated-property when my request includes any other property (e.g. in your case: if name would get passed, either as a route parameter or via the query string), but not when the request does not contain any parameters. In that case, my controller just receives null por MyViewModel. GetValue gets called with the name of the parameter and an empty string, but not for the to-be-populated property (IPAddress in your example). Have you found the reason and perhaps a solution? -

Para mi tuve que agregar IUriValueProviderFactory a mi ValueProviderFactory before it was considered by ASP.NET. -

1 Respuestas

You could have a custom value provider:

public class MyValueProvider : IValueProvider
{
    public bool ContainsPrefix(string prefix)
    {
        return true;
    }

    public ValueProviderResult GetValue(string key)
    {
        if (key.EndsWith("Name"))
        {
            var value = "john";
            return new ValueProviderResult(value, value, CultureInfo.InvariantCulture);
        }
        else if (key.EndsWith("IPAddress"))
        {
            var value = "127.0.0.1";
            return new ValueProviderResult(value, value, CultureInfo.InvariantCulture);
        }

        return null;
    }
}

and a corresponding factory:

public class MyValueProviderFactory : ValueProviderFactory
{
    public override IValueProvider GetValueProvider(ControllerContext controllerContext)
    {
        return new MyValueProvider();
    }
}

que se registrará en Application_Start:

ValueProviderFactories.Factories.Add(new MyValueProviderFactory());

and now you could have a controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View(new MyViewModel());
    }

    [HttpPost]
    public ActionResult Index(MyViewModel model)
    {
        return View(model);
    }
}

y una vista:

@model MyViewModel

@Html.DisplayFor(x => x.Name)
@Html.DisplayFor(x => x.IPAddress)

@using (Html.BeginForm())
{
    <button type="submit">OK</button>
}

Your custom value provider will be used by the default model binder.

Respondido 24 ago 12, 06:08

Perhaps I wasn't clear. I have a custom value provider, but the GetValue method of it wasn't called. I found the answer and will update the question. - Brian Vallelunga

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.