查看或使用helper方法隐藏一个DropDownList在我的Razor视图 [英] View or Hide a dropdownlist in my razor view using helper method

查看:80
本文介绍了查看或使用helper方法隐藏一个DropDownList在我的Razor视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为访问的对象,我定义以下 helper方法(CanBeEdited)表示,如果用户可以编辑对象状态属性或不: -

 公共部分类访问
    {
        公共BOOL CanBeEdited(用户名字符串)
        {返回(!((DoctorID = NULL)及及(DoctorID.ToUpper()等于(username.ToUpper())))及。及(StatusID == 5)); }}}

然后我已指定显示或隐藏某些的DropDownList 上取决于天气的<$ C我的修改视图$ C> CanBeEdited helper方法返回true或false(如果返回true,则用户可以查看和编辑状态的DropDownList ,如果返回假的则认为将呈现一个 @ Html.HiddenFor 重新presenting老的状态值)。

其中包括辅助方法

我的编辑视图看起来如下: -

  @using(Html.BeginForm())
{
    @ Html.ValidationSummary(真)
    &LT;&字段集GT;
        &LT;传奇&GT;参观&LT; /传说&GT;
        &LT; D​​IV CLASS =编辑标记&GT;
            @ Html.LabelFor(型号=&GT; model.Note)
        &LT; / DIV&GT;
        &LT; D​​IV CLASS =主编场&GT;
            @ Html.EditorFor(型号=&GT; model.Note)
            @ Html.ValidationMessageFor(型号=&GT; model.Note)
        &LT; / DIV&GT;
        &LT; D​​IV CLASS =编辑标记&GT;
            @ Html.LabelFor(型号=&GT; model.DoctorID)
        &LT; / DIV&GT;
        &LT; D​​IV CLASS =主编场&GT;
            @ Html.DropDownList(DoctorID的String.Empty)
            @ Html.ValidationMessageFor(型号=&GT; model.DoctorID)
        &LT; / DIV&GT;        @ {
       如果(Model.CanBeEdited(Context.User.Identity.Name))
       {
        &LT; D​​IV CLASS =编辑标记&GT;
            @ Html.LabelFor(型号=&GT; model.StatusID)
        &LT; / DIV&GT;
        &LT; D​​IV CLASS =主编场&GT;
            @ Html.DropDownList(StatusID的String.Empty)
            @ Html.ValidationMessageFor(型号=&GT; model.StatusID)
        &LT; / DIV&GT;
       }
       其他
       {
       @ Html.HiddenFor(型号=&GT; model.StatusID)}
}
        &所述p为H.;
          @ Html.HiddenFor(型号=&GT; model.VisitTypeID)
          @ Html.HiddenFor(型号=&GT; model.CreatedBy)
          @ Html.HiddenFor(型号=&GT; model.Date)
          @ Html.HiddenFor(型号=&GT; model.VisitID)
          @ Html.HiddenFor(型号=&GT; model.PatientID)
          @ Html.HiddenFor(型号=&GT; model.timestamp)        &LT;输入类型=提交值=创建/&GT;        &所述; / P&GT;
    &LT; /字段集&GT;
}

说实话这是我第一次实施,如情况下,, 所以我的做法听起来有效?,,或者它有一些弱点,我不知道的?。因为我需要围绕实现我所有的Web应用程序类似案件......

霸菱记住,我也检查了CanBeEdited的行动方法..

感谢您事先的任何帮助。

更新: -
我的岗位操作方法看起来如下: -

  [HttpPost]
        公众的ActionResult编辑(参观访问)
        {
            如果(!(visit.Editable(User.Identity.Name)))
            {
                返回视图(NOTFOUND);
            }
            尝试
            {
                如果(ModelState.IsValid)
                {
                    repository.UpdateVisit(访问);
                    repository.Save();
                    返回RedirectToAction(「指数」);
                }
            }
            赶上(DbUpdateConcurrencyException前)
            {
                变种条目= ex.Entries.Single();
                VAR clientValues​​ =(访问)entry.Entity;                ModelState.AddModelError(的String.Empty,你试图编辑记录
                +是由其他用户修改你得到了原值后的
                +编辑操作被取消,并在数据库中的当前值
                +已经显示出来。如果你仍然想要编辑这个记录,点击
                +保存按钮,否则再次点击返回列表超链接。);
                // patient.timestamp = databaseValues​​.timestamp;
            }            赶上(DataException)
            {
                //记录错误(例外后添加一个变量名)
                ModelState.AddModelError(的String.Empty,无法保存更改再试一次,如果问题仍然存在,与系统管理员联系。);
            }
            ViewBag.DoctorID =新的SelectList(Membership.GetAllUsers(),用户名,用户名,visit.DoctorID);
            ViewBag.StatusID =新的SelectList(db.VisitStatus,StatusID,说明,visit.StatusID);
            ViewBag.VisitTypeID =新的SelectList(db.VisitTypes,VisitTypeID,说明,visit.VisitTypeID);
            返回视图(访问);
        }


解决方案

我不觉得并称在View是一个好主意。我想有我的视图模型持有布尔类型的属性,以确定它是可编辑与否。 ,你可以检查相关权限后在控制器中设置的值。

 公共类ProductViewModel
{
  公共BOOL IsEditable {集;获取;}
  //其他相关属性
}

和控制器动作

 公众的ActionResult GetProduct()
{
  ProductViewModel objVM =新ProductViewModel();
  objVm.IsEditable = CheckPermissions();}
私人布尔CheckPermissions()
{
  //检查的条件和返回true或false;
}

所以认为会干净像部份

  @if(Model.IsEditable)
{
  //标记,对于可编辑区域
}

I have an object named Visit and i defined the following helper method ("CanBeEdited") to specify if users can edit the object Status property or not:-

public partial class Visit 
    {
        public bool CanBeEdited(string username)
        {return (((DoctorID != null) && (DoctorID.ToUpper().Equals(username.ToUpper()))) && (StatusID == 5));       }     }}

Then i have specified to show or hide certain dropdownlist on my Edit view depending on weather the CanBeEdited helper method returns true or false (if it returns true then the user can view and edit the Status dropdownlist, and if it returns false then the view will render an @Html.HiddenFor representing the old status value).

My edit view which includes the helper method looks as following:-

@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Visit</legend>
        <div class="editor-label">
            @Html.LabelFor(model => model.Note)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Note)
            @Html.ValidationMessageFor(model => model.Note)
        </div>


        <div class="editor-label">
            @Html.LabelFor(model => model.DoctorID)
        </div>
        <div class="editor-field">
            @Html.DropDownList("DoctorID", String.Empty)
            @Html.ValidationMessageFor(model => model.DoctorID)
        </div>

        @{
       if (Model.CanBeEdited(Context.User.Identity.Name))
       {
        <div class="editor-label">
            @Html.LabelFor(model => model.StatusID)
        </div>
        <div class="editor-field">
            @Html.DropDownList("StatusID", String.Empty)
            @Html.ValidationMessageFor(model => model.StatusID)
        </div>
       }
       else
       {
       @Html.HiddenFor(model => model.StatusID)}
}
        <p>
          @Html.HiddenFor(model => model.VisitTypeID)
          @Html.HiddenFor(model => model.CreatedBy)
          @Html.HiddenFor(model => model.Date)
          @Html.HiddenFor(model => model.VisitID)
          @Html.HiddenFor(model => model.PatientID)
          @Html.HiddenFor(model => model.timestamp)

        <input type="submit" value="Create" />

        </p>
    </fieldset>
}

To be honest it is the first time i implement such as case,, so is my approach sound valid ???,, or it have some weaknesses i am unaware of ??. As i need to implemented similar cases all around my web application...

Baring in mind that i am also checking for the CanBeEdited on the action methods..

Thanks in advance for any help.

Updated:- My post action method look as follow:-

 [HttpPost]
        public ActionResult Edit(Visit visit)
        {
            if (!(visit.Editable(User.Identity.Name)))
            {
                return View("NotFound");
            }
            try
            {
                if (ModelState.IsValid)
                {
                    repository.UpdateVisit(visit);
                    repository.Save();
                    return RedirectToAction("Index");
                }
            }
            catch (DbUpdateConcurrencyException ex)
            {
                var entry = ex.Entries.Single();
                var clientValues = (Visit)entry.Entity;

                ModelState.AddModelError(string.Empty, "The record you attempted to edit "
                + "was modified by another user after you got the original value. The "
                + "edit operation was canceled and the current values in the database "
                + "have been displayed. If you still want to edit this record, click "
                + "the Save button again. Otherwise click the Back to List hyperlink.");
                //   patient.timestamp = databaseValues.timestamp;
            }

            catch (DataException)
            {
                //Log the error (add a variable name after Exception)
                ModelState.AddModelError(string.Empty, "Unable to save changes. Try again, and if the problem persists contact your system administrator.");
            }
            ViewBag.DoctorID = new SelectList(Membership.GetAllUsers(), "Username", "Username", visit.DoctorID);
            ViewBag.StatusID = new SelectList(db.VisitStatus, "StatusID", "Description", visit.StatusID);
            ViewBag.VisitTypeID = new SelectList(db.VisitTypes, "VisitTypeID", "Description", visit.VisitTypeID);
            return View(visit);
        }

解决方案

I don't feel adding that in the View is a good idea. I would like to have My ViewModel to hold a property of boolean type to determine that it is editable or not. The value of that you can set in your controller after checking the relevant permissions.

public class ProductViewModel
{
  public bool IsEditable { set;get;}
  //other relevant properties
}

and controller action

public ActionResult GetProduct()
{
  ProductViewModel objVM=new ProductViewModel();
  objVm.IsEditable=CheckPermissions();

}
private bool CheckPermissions()
{
  //Check the conditions and return true or false;
}

So view will be clean like ths

@if (Model.IsEditable)
{
  //Markup for editable region
}

这篇关于查看或使用helper方法隐藏一个DropDownList在我的Razor视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆