2016-05-05 3 views
0

Mein System lässt Benutzer Bestellungen aufgeben und möchte, dass sie diese Aufträge bearbeiten können, aber ich plane eine Auftragsstatusvariable zu dieser Bearbeitung hinzuzufügen form, wie kann ich es so machen, dass NUR Mitarbeiter dies sehen können. Ich habe versucht, mit @if (ViewContext.HttpContext.User.IsInRole aber dies schien nicht. MethodenNur einem bestimmten Benutzertyp erlauben, ein Feld in einer Bearbeitungsansicht zu sehen

bearbeiten

// GET: Orders/Edit/5 
     public ActionResult Edit(int? id) 
     { 
      if (id == null) 
      { 
       return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 
      } 
      CustomerOrder order = db.CustomerOrders.Find(id); 
      if (order == null) 
      { 
       return HttpNotFound(); 
      } 
      return View(order); 
     } 

      // POST: Orders/Edit/5 
     [HttpPost] 
     [ValidateAntiForgeryToken] 
     public ActionResult Edit(CustomerOrder order) 
     { 
      if (ModelState.IsValid) 
      { 
       db.Entry(order).State = EntityState.Modified; 
       db.SaveChanges(); 
       return RedirectToAction("Index"); 
      } 
      return View(order); 
     } 

bearbeiten Ansicht

@model bob.Models.CustomerOrder 
@{ 
    ViewBag.Title = "Edit"; 
} 
<h2>Edit</h2> 

@using (Html.BeginForm()) 
{ 
    @Html.AntiForgeryToken() 

    <div class="form-horizontal"> 

     <h4>Order</h4> 
     <hr /> 
     @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
     @Html.HiddenFor(model => model.Id) 
     <div class="form-group"> 
      @Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" }) 
      </div> 
     </div> 
     <div class="form-group"> 
      @Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" }) 
      </div> 
     </div> 
     <div class="form-group"> 
      @Html.LabelFor(model => model.Address, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Address, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Address, "", new { @class = "text-danger" }) 
      </div> 
     </div> 
     <div class="form-group"> 
      @Html.LabelFor(model => model.City, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.City, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.City, "", new { @class = "text-danger" }) 
      </div> 
     </div> 
     <div class="form-group"> 
      @Html.LabelFor(model => model.State, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.State, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.State, "", new { @class = "text-danger" }) 
      </div> 
     </div> 
     <div class="form-group"> 
      @Html.LabelFor(model => model.PostalCode, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.PostalCode, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.PostalCode, "", new { @class = "text-danger" }) 
      </div> 
     </div> 
     <div class="form-group"> 
      @Html.LabelFor(model => model.Country, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Country, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Country, "", new { @class = "text-danger" }) 
      </div> 
     </div> 
     <div class="form-group"> 
      @Html.LabelFor(model => model.Phone, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Phone, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Phone, "", new { @class = "text-danger" }) 
      </div> 
     </div> 


     <div class="form-group"> 
      @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
      @if (ViewContext.HttpContext.User.IsInRole("Stores Manager")) 
      { 
       @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" }) 
      } 
      </div> 
     </div> 
     <div class="form-group"> 
      <div class="col-md-offset-2 col-md-10"> 
       <input type="submit" value="Save" /> 
      </div> 
     </div> 
    </div> 
} 
<div> 
    @Html.ActionLink("Back to List", "Index") 
</div> 
@section Scripts { 
    @Scripts.Render("~/bundles/jqueryval") 
} 

Bitte beachten Sie die Reihenfolge Statusvariablen zu arbeiten Ich möchte das mit machen ist noch kein Ding, aber wenn Sie mir zeigen könnten, wie man es mit einer der anderen Variablen macht, wäre ich dankbar.

+0

zu arbeiten, wenn Sie „Variable“ sagen, meinen Sie " Eigenschaft "Ihres Modells? – Balde

+0

Ja, ich mache Balde. –

Antwort

0

Hallo Jungs haben es durch die Arbeit nicht, es zu tun haben ausgiebig getestet, um Probleme, die mir zu sagen, fühlen sich frei, entstehen könnten aber dies scheint

<div class="form-group"> 
      @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
      @if (User.IsInRole("Stores Manager")) 
      { 
       @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" }) 
      } 
      else 
      { 
       @Html.HiddenFor(model => model.Email) 
      } 
      </div> 
0

Ansichten haben eine IPrincipal Eigenschaft namens User:

@if (User.IsInRole("Stores Manager")) 
{ 
    @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } }) 
    @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" }) 
} 

Wenn Sie das Formular abschicken, wenn der Benutzer nicht in Store Manager Rolle ist, dann haben diese Eigenschaften nicht in Ihrem Modell erforderlich.

+0

Dies geschieht jedoch, wenn ein Benutzer seine Bestellung bearbeitet. Bleibt die E-Mail so wie sie war –

+0

Lass mich dir einen kleinen Zusammenhang geben. Ich möchte es nicht mit E-Mail tun, die Eigenschaft, die ich mit OrderStatus machen möchte, die ich nicht hinzugefügt habe, wird den Wert 1 an der Kasse erhalten, 1 entspricht dem OrderStatus "Ordered" Und ich möchte, dass es so bleibt, es sei denn ein Mitarbeiter bearbeitet es # –

+0

Das hängt davon ab, wie Sie Ihr Modell speichern. Wenn ein NOT-Benutzer "Store-Manager" das Formular absendet, ist "E-Mail" auf dem Parameter ungültig, der das Modell in Ihrer Aktion Ihres Controllers enthält. Wenn Sie dann die Entität 'CustomerOrder' direkt speichern, die auf dem Parameter erscheint, wird' Email' auf Null gesetzt. Gleiches gilt für "OrderStatus". – Balde

Verwandte Themen