如何禁用@ html.Editor()以使其为只读

时间:2018-07-24 22:17:31

标签: asp.net-mvc razor editor

我试图根据用户权限(MVC版本5.2.3)使某些字段可编辑,并使某些字段为只读。我可以在@ html.EditorFor()这个主题上看到很多答案,但不是普通的@ html.Editor()。我尝试了以下方法,但都没有产生只读字段:

    @Override
    public void onClick(View v) {
        String poster_id = event_picture.getProfileId();
        DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference()
                .child("events").child("userid").child(poster_id);
        if (databaseReference != null) databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                Event event = dataSnapshot.getValue(Event.class);
                String clickedEventName = event_name.getText().toString();
                String clickedEventLocation = event_location.getText().toString();
                String eventName = event.getTitle();
                String eventAddress = event.getAddress();

                if (clickedEventLocation.equals(eventAddress) && clickedEventName.equals(eventName)){
                    displayEvent(event);
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }
}

public static void displayEvent(Event e){
    Bundle bundle = getBundle(e);
    DisplayEvent displayEvent = new DisplayEvent();
    displayEvent.setArguments(bundle);
    AppCompatActivity context = new AppCompatActivity();
    FragmentManager fragmentManager = context.getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.setCustomAnimations(android.R.anim.slide_in_left,
            android.R.anim.slide_out_right);
    fragmentTransaction.replace(R.id.fragment_display, displayEvent, "display_event");
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();
}

有人可以帮助我吗?非常感谢。

2 个答案:

答案 0 :(得分:0)

您可以编写自己的扩展名以接收布尔属性"disabled"作为参数:

public static class HtmlExtensions
{
   public static MvcHtmlString EditorDisabled<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, bool disabled, object htmlAttributes = null)
  {
    return EditorDisabled(htmlHelper, expression, disabled, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
  }

  public static MvcHtmlString EditorDisabled<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, bool disabled, IDictionary<string, object> htmlAttributes)
  {
    if (htmlAttributes == null)
    htmlAttributes = new Dictionary<string, object>();
    if (disabled)
    htmlAttributes["disabled"] = "disabled";
    return htmlHelper.Editor(expression, htmlAttributes);
  }
}

答案 1 :(得分:0)

您可以将其从编辑器更改为:

@Html.DisplayFor(model => model.Title)

它只会从模型中提取值。从那里,您只需要在剃刀中实施关于谁可以看到什么的逻辑。例如:

if(Request.IsAuthenticated){
    if (isAdmin){
                @Html.Editor(model => model.Title)
                }
    if (isUser){
                @Html.DisplayFor(model => model.Title)
                }  
   }

我希望这有助于您走上正确的道路!