2015-03-04 2 views
0

У меня есть типичное представление основных деталей, и мне нужно выполнить удаленную проверку на свойстве Name моего BarViewModel.Параметры привязки при удаленной проверке

мой взгляд модели:

public class FooViewModel 
{ 
    public IEnumerable<BarViewModel> Bars { get; set; } 
} 

public class BarViewModel 
{ 
    [Required] 
    [Remote("CheckName", "Foo")] 
    public string Name { get; set } 
} 

Контроллер:

public class FooController 
{ 
    public ActionResult Edit() 
    { 
      var viewModel = new FooViewModel 
      { 
       Bars = new List<BarViewModel> { new BarViewModel() } 
      }; 
      return View(viewModel); 
    } 

    // For remote validation 
    public ActionResult CheckName(/*[Bind(Prefix = "???")] */string name) 
    { 
      // Parameter binding failed 
    } 
} 

FooViewModel Изменить вид:

@model FooViewModel 

@using (Html.BeginForm()) 
{ 
    @Html.EditorFor(m => m.Bars) 
} 

BarViewmodel EditorTemplate:

@model BarViewModel 

<div class="form-group"> 
    @Html.LabelFor(m => m.Name, new { @class = "control-label col-md-2 }) 
    <div class="col-md-10"> 
     @Html.EditorFor(m => m.Name, new { htmlAttributes = new { @class = "form-control" } }) 
     @HtmlValidationMessageFor(m => m.Name, string.Empty, new { @class = "text-danger" }) 
    </div> 
</div> 

Проблема заключается в том, что BarViewModel.Name отправлен для удаленной проверки, параметр name в методе CheckName всегда возвращает null. Я пробовал, включая атрибут Bind с Prefix такими как Bars и Bars[0], но параметр name по-прежнему null. Может кто-нибудь помочь?

+0

я уже ранее сообщали сообщили об этом как об ошибке на Codeplex (см [http://stackoverflow.com/questions/27513472/remote-validation-for-list -of-модели/27517407 # 27517407] (http://stackoverflow.com/questions/27513472/remote-validation-for-list-of-models/27517407#27517407)) –

ответ

0

Fixed его помощью BindAttribute:

public class FooController 
{ 
    public ActionResult Edit() 
    { 
      var viewModel = new FooViewModel 
      { 
       Bars = new List<BarViewModel> { new BarViewModel() } 
      }; 
      return View(viewModel); 
    } 

    // For remote validation 
    public ActionResult CheckName([Bind(Prefix = "Bars[0].Name")] string name) 
    { 
      // Perform remote validation 
    } 
} 
Смежные вопросы