4

我有一个控制器动作如下

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. 有没有办法在不创建另一个视图的情况下做到这一点?

4

2 回答 2

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);
}
于 2013-07-23T11:15:40.710 回答
-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 

} else { // 显示代码

}

在控制器中,如下所示

  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);


    }
于 2013-07-23T12:35:19.503 回答