ValueProvider no llamado para propiedad que no está en forma
Frecuentes
Visto 773 veces
1
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.
1 Respuestas
2
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 asp.net-mvc model-binding or haz tu propia pregunta.
Did you register your custom value provider factory? - Eranga
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: ifname
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 receivesnull
porMyViewModel
.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? - Vincent SelsPara mi tuve que agregar
IUriValueProviderFactory
a miValueProviderFactory
before it was considered by ASP.NET. - binki