0

我有一个容器和嵌套的视图模型,并使用 EditorFor 来呈现嵌套的视图模型,我想将 remoteAttribute 添加到一个视图模型(ConcreteViewModelA :: prop3)中的一个属性中以进行验证。但是,在验证控制器操作方法上,我得到的只是空值。

我尝试使用 Validate([Bind(Prefix="item")]string prop3),但仍然返回为 NULL。有任何想法吗?

public class SomeContainer
{
    public List<ISomethingViewModel> SomeViewModels { get; set; }
}

public class ConcreteViewmodelA : ISomethingViewModel
{
    public int prop1 { get; set; }
    public int prop2 { get; set; }
    [Remote("Validate", "RemoteValidation")]
    public string prop3 { get; set; }
}

public class ConcreteViewModelB : ISomethingViewModel
{
    public int prop1 { get; set; }
    public int prop2 { get; set; }
}

public interface ISomethingViewModel
{
    int prop1 { get; set; }
    int prop2 { get; set; }
}

看法:

@model test.Models.SomeContainer

@using (Html.BeginForm())
{
    @Html.ValidationSummary()
    <p>Begin here</p> 
    foreach (var item in Model.SomeViewModels)
    {
        @Html.EditorFor(x => item)
    }        
}
4

2 回答 2

2

尝试定义一个视图模型:

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

进而:

public ActionResult Validate([Bind(Prefix = "item")] MyViewModel model)
{
    return Json(
        !string.IsNullOrEmpty(model.Prop3), 
        JsonRequestBehavior.AllowGet
    );
}
于 2012-06-22T06:27:42.063 回答
0

检查萤火虫。您的 url 请求如下所示Validate?item.prop3=

所以你可以做这样的事情来读取值

    public ActionResult Validate(string prop3)
    {
        string prop3Val = Request.QueryString["item.prop3"].ToString();

        //your operations with prop3Val
        return Json(prop3Val, JsonRequestBehavior.AllowGet);
    }

在此处输入图像描述

于 2012-06-22T06:28:13.463 回答