2017-05-15 1 views
0

Ich habe eine ASP. Net Core 1.1 MVC Web App. Die App spricht mit einem Web-API-Backend, um Daten im Datenspeicher zu lesen/schreiben.Ausgewählte Option von SELECT von View zurück zum Controller

In der Web-App habe ich eine Bearbeitungsseite, auf der Sie einen Datensatz eines bestimmten Objekts bearbeiten können. Eines der Felder in dem Datensatz ist eine Dropdown-Liste (d. H. Ein HTML-Tag). Mein Problem ist, dass ich nicht weiß, wie man die ausgewählte Option von der Dropdown-Liste (in der Ansicht) zurück zum Controller bringt.

Um Ihnen einen kurzen Hintergrund zu geben, wird diese Web-App von Immobilienmaklern verwendet, um ihre Eigenschaften zu verwalten. Um die Dinge einfach, denn jetzt hat die Eigenschaft, eine Grundstruktur - eine ID und die Adresse und eine „Art der Unterkunft“ (Haus, Wohnung, Büro, usw.)

So habe ich diese beiden Modelle:

using System.Collections.Generic; 
using System.ComponentModel.DataAnnotations; 
using System.ComponentModel.DataAnnotations.Schema; 

namespace InspectionsData.Models 
{ 
    public partial class PropertyType 
    { 
     [Key] 
     [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
     public int Id { get; set; } 
     public string PropertyTypeName { get; set; } // e.g. house, flat, office, etc. 
    } 
} 

und

using System; 
using System.ComponentModel.DataAnnotations; 
using System.ComponentModel.DataAnnotations.Schema; 

namespace InspectionsData.Models 
{ 
    [Table("property")] 
    public class Property 
    { 

     [Key] 
     [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
     public int? Id { get; set; } 
     public string Street { get; set; } 
     public string City { get; set; } 
     public string Region { get; set; } 
     public string Country { get; set; } 
     public int? PropertyTypeId { get; set; } 
     [ForeignKey("PropertyTypeId")] 
     public PropertyType PropertyType { get; set; } 
    } 
} 

I dann ein Ansichtsmodell erstellt, so dass ich beide der oben genannten Modelle zur Ansicht einspeisen:

using InspectionsData.Models; 
using Microsoft.AspNetCore.Mvc.Rendering; 

namespace InspectionsTestClient.ViewModels 
{ 
    public class PropertyIndexViewModel 
    { 
     public Property Property { get; set; } 
     public SelectList PropertyTypes { get; set; } 
    } 
} 

Dann habe ich eine Steuerung, die die Ansicht auffüllt:

// GET: Property/Edit/5 
     public async Task<ActionResult> Edit(int id) 
     { 
      string apiUrl = "http://localhost:50082/api/"; 

      if (!ModelState.IsValid) 
      { 
       return BadRequest(ModelState); 
      } 

      //Read property from Web API    
      string url = apiUrl + "Properties/" + id.ToString(); 
      HttpResponseMessage responseMessage = await client.GetAsync(url); 
      if (responseMessage.IsSuccessStatusCode) 
      { 
       var responseData = responseMessage.Content.ReadAsStringAsync().Result; 
       var prop = Newtonsoft.Json.JsonConvert.DeserializeObject<Property>(responseData); 
       if (prop == null) 
       { 
        return NotFound(); 
       } 

       List<PropertyType> propertyTypes = null; 
       url = apiUrl + "PropertyTypes"; 
       responseMessage = await client.GetAsync(url); 
       if (responseMessage.IsSuccessStatusCode) 
       { 
        responseData = responseMessage.Content.ReadAsStringAsync().Result; 
        propertyTypes = Newtonsoft.Json.JsonConvert.DeserializeObject<List<PropertyType>>(responseData); 
       } 

       PropertyIndexViewModel vm = new PropertyIndexViewModel() 
       { 
        Property = prop, 
        PropertyTypes = new SelectList(propertyTypes, "Id", "PropertyTypeName") 
       }; 

       return View(vm); 
      } 

      return View("Error"); 
     } 

Ich weiß, es bei der Herstellung von zwei Aufrufen der API ineffizient ist - eine Immobilie zu erhalten und eine Liste der Objekttypen zu bekommen - ich dies verbessern wird später das ist, aber jetzt, wie es ist ... und die Aussicht:

@model InspectionsTestClient.ViewModels.PropertyIndexViewModel 

@{ 
    ViewData["Title"] = "Edit"; 
} 

@{ 
    Layout = "_Layout"; 
} 

<h2>Edit</h2> 

@Html.ValidationSummary(); 

<form asp-action="Edit"> 
    <div class="form-horizontal"> 
     <h4>Property</h4> 
     <hr /> 
     <div asp-validation-summary="ModelOnly" class="text-danger"></div> 
     <input type="hidden" asp-for="Property.Id" /> 
     <div class="form-group"> 
      <label asp-for="Property.Street" class="col-md-2 control-label"></label> 
      <div class="col-md-10"> 
       <input asp-for="Property.Street" class="form-control" /> 
       <span asp-validation-for="Property.Street" class="text-danger"></span> 
      </div> 
     </div> 
     <div class="form-group"> 
      <label asp-for="Property.City" class="col-md-2 control-label"></label> 
      <div class="col-md-10"> 
       <input asp-for="Property.City" class="form-control" /> 
       <span asp-validation-for="Property.City" class="text-danger"></span> 
      </div> 
     </div> 
     <div class="form-group"> 
      <label asp-for="Property.Region" class="col-md-2 control-label"></label> 
      <div class="col-md-10"> 
       <input asp-for="Property.Region" class="form-control" /> 
       <span asp-validation-for="Property.Region" class="text-danger"></span> 
      </div> 
     </div> 
     <div class="form-group"> 
      <label asp-for="Property.Country" class="col-md-2 control-label"></label> 
      <div class="col-md-10"> 
       <input asp-for="Property.Country" class="form-control" /> 
       <span asp-validation-for="Property.Country" class="text-danger"></span> 
      </div> 
     </div> 
     <div class="form-group"> 
      <label asp-for="Property.PropertyType" class="col-md-2 control-label"></label> 
      <div class="col-md-10"> 
       <select asp-for="Property.PropertyType" asp-items="@Model.PropertyTypes"></select> 
      </div> 
     </div> 
     <div class="form-group"> 
      <div class="col-md-offset-2 col-md-10"> 
       <input type="submit" value="Save" class="btn btn-default" /> 
      </div> 
     </div> 
    </div> 
</form> 

<div> 
    <a asp-action="Index">Back to List</a> 
</div> 

@section Scripts { 
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");} 
} 

Wenn der Benutzer die SAVE-Taste auf dem Formular klickt, es wieder zu einer anderen Controller-Aktion Beiträge:

// POST: Property/Edit/5 
     [HttpPost] 
     [ValidateAntiForgeryToken] 
     public async Task<ActionResult> Edit(int id, Property prop) 
     { 
      try 
      { 
       if (ModelState.IsValid) 
       { 
        string apiUrl = "http://localhost:50082/api/";      
        string url = apiUrl + "properties/" + id.ToString(); 
        string jsonInString = JsonConvert.SerializeObject(prop);      
        HttpContent content = new StringContent(jsonInString, Encoding.UTF8, "application/json"); 
        HttpResponseMessage responseMessage = await client.PutAsync(url, content); 
        if (responseMessage.IsSuccessStatusCode) 
        { 
         return RedirectToAction("Index"); 
        }      
       } 

       return View(); 
      } 

      catch 
      { 
       return View(); 
      } 
     } 

Diese Controller-Aktion sendet also die aktualisierte Eigenschaft an die Web-API. Das Problem ist, dass alles 100% funktioniert, bis es zu dieser finalen Controller-Aktion kommt - wenn ich den "prop" -Parameter inspiziere, enthält es alle vorgenommenen Änderungen, aber es enthält nicht den ausgewählten PropertyType - wie kann ich den ausgewählten PropertyType zurückgeben aus der Sicht zurück zum Controller? Dank ...

Antwort

Verwandte Themen