繁体   English   中英

在ASP.net MVC中使用相同的视图进行编辑和查看

[英]Utilizing the same view for edit and view in ASP.net MVC

我有一个控制器动作如下

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 有没有办法在不创建另一个视图的情况下执行此操作?

模型:

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

模型

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


    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM