我有一个clientModel:
public class ClientViewModel
{
public string Type {get; set;}
public AddSpecificitiesViewModel dep {get; set;}
public AddSpecificitiesViewModel dest {get; set;}
}
AddSpecificitiesViewModel如下:
public class AddSpecificitiesViewModel
{
public string Country { get; set; }
public SelectList CountryList { get; set; }
}
在加载屏幕时,我的ClientController的操作点击了:
public ActionResult Index()
{
ClientViewModel client = new ClientViewModel();
client.Type = "Des";
if (client.Type == "Des"){
//Config function create a new AddSpecificitiesViewModel with the correct values
Config(dep)
client.dest = null;
} else if (client.Type == "Dest"){
Config(dest)
client.dep = null;
}
}
在我看来,我有followind字段:
@using (Html.BeginForm("Index", "Client"}))
{
<div>
@Html.LabelFor(model => model.TypeOfService)
@Html.HiddenFor(model => model.TypeOfService)
</div>
<h3>Departure</h3>
<div>
@Html.LabelFor(model => model.dep.Country)
@Html.DropDownListFor(model => model.dep.Country, Model.dep.CountryList, "")
</div>
<h3>Destination</h3>
<div>
@Html.LabelFor(model => model.dest.Country)
@Html.DropDownListFor(model => model.dest.Country, Model.dest.CountryList, "")
</div>
}
那么如何根据ClientViewModel的值Type显示Departure部分或Destination部分。
如何使用流畅验证进行验证? 为了流利的验证,我做了类似的事情:
RuleFor(obj => obj.dest).NotNull().WithLocalizedMessage(typeof(BStrings), "Error").Unless(obj => obj.Type == "Dest" || obj.TypeOfService == "Other");
RuleFor(obj => obj.dep).NotNull().WithLocalizedMessage(typeof(BStrings), "Error").Unless(obj => obj.Type == "Dep" || obj.TypeOfService == "Other");
答案 0 :(得分:1)
简单地做
@if (Model.Type.ToUpper().Equals("DES") && Model.dep!=null)
{
<h3>Departure</h3>
<div>
@Html.LabelFor(model => model.dep.Country)
@Html.DropDownListFor(model => model.dep.Country, Model.dep.CountryList, "")
</div>
}
@if(Model.Type.ToUpper().Equals("DEST") && Model.dest!=null)
{
<h3>Destination</h3>
<div>
@Html.LabelFor(model => model.dest.Country)
@Html.DropDownListFor(model => model.dest.Country, Model.dest.CountryList, "")
</div>
}
答案 1 :(得分:0)
使用if语句仅呈现您想要的内容:
@using (Html.BeginForm("Index", "Client"}))
{
<div>
@Html.LabelFor(model => model.TypeOfService)
@Html.HiddenFor(model => model.TypeOfService)
</div>
@if (model.Type == "DES")
{
<h3>Departure</h3>
<div>
@Html.LabelFor(model => model.dep.Country)
@Html.DropDownListFor(model => model.dep.Country, Model.dep.CountryList, "")
</div>
}
else
{
<h3>Destination</h3>
<div>
@Html.LabelFor(model => model.dest.Country)
@Html.DropDownListFor(model => model.dest.Country, Model.dest.CountryList, "")
</div>
}
}