加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 21:53:38 所属栏目:百科 来源:网络整理
导读:我有一个控制器动作如下 public ActionResult OpenForm(){ return View("Index");} 我的视图如下[Index.cshtml] @model BusinessProcess.Models.HelloworldTO@using (Html.BeginForm()){ @Html.ValidationSummary(true) @Html.DisplayNameFor(model = model.
我有一个控制器动作如下

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读