我有一个控制器动作如下
public ActionResult OpenForm()
{
return View("Index");
}
我的观点如下[Index.cshtml]
@model BusinessProcess.Models.HelloworldTO
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
@Html.DisplayNameFor(model => model.Response_Borrower)
@Html.EditorFor(model => model.Response_Borrower)
}
现在问题是我对“编辑”和“查看”都使用相同的视图。现在,在某些情况下,我希望用户仅“查看”数据并将@Html.EditorFor
转换为@Html.DisplayFor
。有没有办法在不创建另一个视图的情况下做到这一点?
答案 0 :(得分:2)
型号:
public class HelloworldTO()
{
public bool Edit {get; set;}
}
查看:
@model BusinessProcess.Models.HelloworldTO
@if (Model.Edit)
{
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
@Html.DisplayNameFor(model => model.Response_Borrower)
@Html.EditorFor(model => model.Response_Borrower)
}
}
else
{
@Html.DisplayFor(model => model.Response_Borrower)
}
控制器
public ActionResult OpenForm()
{
HelloworldTO model = new HelloworldTO ();
model.Edit = /*certain circumstances*/;
return View("Index", model);
}
答案 1 :(得分:-1)
模型
public class Model1
{
public bool SameView { get; set; }
public bool Response_Borrower { get; set; }
}
查看
@model CodeProjectAnswers.Models.Model1
@if(Model.SameView) {
// Do what you want
} 其他 { //显示代码
}
并且在控制器中有些如下
public ActionResult Sample()
{
Model1 model = new Model1();
if (model.SameView)
{
// Set it to false and what is ur condition
}
else
{
model.SameView = true;
}
return View("Sample", model);
}