我在ASP.NET Core MVC中製作了一個基本的CRUD應用程序,允許用戶與多個電視頻道進行交互。當用戶點擊用於特定頻道的「編輯」按鈕,該代碼運行:在ASP.NET Core MVC視圖中,如何訪問當前模型的屬性?
<a asp-action="Edit" asp-route-id="@Html.Raw(channel.ID)">Edit</a>
接下來,用戶被帶到所述「編輯」視圖。該視圖包含每個可編輯屬性的表單。這裏是「描述」字段的例子:
<div class="form-group">
<label asp-for="Desc" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="Desc" class="form-control"/>
<span asp-validation-for="Desc" class="text-danger"></span>
</div>
</div>
該字段應包含現有的描述,讓用戶不必記住它是什麼。我怎樣才能將這些信息從「索引」視圖傳遞到這個視圖?
編輯
每請求時,這裏是我的全部控制器:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.IO;
using Newtonsoft.Json;
using TV_Channel_Manager.Models;
using System.Text.RegularExpressions;
namespace TV_Channel_Manager.Controllers
{
public class ChannelController : Controller
{
// GET: Channel
public async Task<ActionResult> Index()
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:57412/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes("Administrator:")));
HttpResponseMessage response = await client.GetAsync("config/v1/project/channels");
if (response.IsSuccessStatusCode)
{
string responseStr = await response.Content.ReadAsStringAsync();
// change keys so that they match props, and convert to object
List<Channel> channels = JsonConvert.DeserializeObject<List<Channel>>(responseStr.Replace("PROJECT_ID", "ProjID").Replace("common.ALLTYPES_DESCRIPTION", "Desc").Replace("common.ALLTYPES_NAME", "Name").Replace("servermain.CHANNEL_DIAGNOSTICS_CAPTURE", "CaptureDiag").Replace("servermain.CHANNEL_NON_NORMALIZED_FLOATING_POINT_HANDLING", "NonNormalizedFloatHandling").Replace("servermain.CHANNEL_UNIQUE_ID", "ID").Replace("servermain.CHANNEL_WRITE_OPTIMIZATIONS_DUTY_CYCLE", "WriteOptimDutyCycle").Replace("servermain.CHANNEL_WRITE_OPTIMIZATIONS_METHOD", "WriteOptimMethod").Replace("servermain.MULTIPLE_TYPES_DEVICE_DRIVER", "Driver").Replace("simulator.CHANNEL_ITEM_PERSISTENCE", "ItemPersistence").Replace("simulator.CHANNEL_ITEM_PERSISTENCE_DATA_FILE", "ItemPersistenceFile"));
ViewData["channels"] = channels; // pass object list to view
}
}
return View();
}
// GET: Channel/Details/5
public ActionResult Details(int id)
{
return View();
}
// GET: Channel/Create
public ActionResult Create()
{
return View();
}
// POST: Channel/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(IFormCollection collection)
{
try
{
// TODO: Add insert logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
// GET: Channel/Edit/5
public ActionResult Edit(int id)
{
return View();
}
// POST: Channel/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Edit(long projID, string desc, string name, bool captureDiag, int nonNormalizedFloatHandling, long ID, int writeOptimDutyCycle, int writeOptimMethod, string driver, bool itemPersistence, string itemPersistenceFile)
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:57412/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes("Administrator:")));
Channel newChannelObj = new Channel(projID, desc, name, captureDiag, nonNormalizedFloatHandling, ID, writeOptimDutyCycle, writeOptimMethod, driver, itemPersistence, itemPersistenceFile);
// replace prop names with actual names
string serialized = JsonConvert.SerializeObject(newChannelObj).Replace("\"ProjID\"", "\"PROJECT_ID\"").Replace("\"ID\"", "\"servermain.CHANNEL_UNIQUE_ID\"").Replace("Desc", "common.ALLTYPES_DESCRIPTION").Replace("Name", "common.ALLTYPES_NAME").Replace("CaptureDiag", "servermain.CHANNEL_DIAGNOSTICS_CAPTURE").Replace("NonNormalizedFloatHandling", "servermain.CHANNEL_NON_NORMALIZED_FLOATING_POINT_HANDLING").Replace("WriteOptimDutyCycle", "servermain.CHANNEL_WRITE_OPTIMIZATIONS_DUTY_CYCLE").Replace("WriteOptimMethod", "servermain.CHANNEL_WRITE_OPTIMIZATIONS_METHOD").Replace("Driver", "servermain.MULTIPLE_TYPES_DEVICE_DRIVER").Replace("\"ItemPersistence\"", "\"simulator.CHANNEL_ITEM_PERSISTENCE\"").Replace("\"ItemPersistenceFile\"", "\"simulator.CHANNEL_ITEM_PERSISTENCE_DATA_FILE\"");
StringContent newChannel = new StringContent(serialized, Encoding.UTF8, "application/json");
HttpResponseMessage response = await client.PutAsync("config/v1/project/channels/" + name, newChannel);
if (response.IsSuccessStatusCode)
return View();
}
return NotFound();
}
// GET: Channel/Delete/5
public ActionResult Delete(int id)
{
return View();
}
// POST: Channel/Delete/5
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Delete(int id, IFormCollection collection)
{
try
{
// TODO: Add delete logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
}
}
現在訪問屬性,我建議你做詳細視圖(這將解決如何獲取現有的詳細信息並將其顯示在頁面上)。編輯將幾乎完全相同,當您使用編輯GET時,您可以拉下現有的詳細信息並將其顯示在頁面上。 – MindingData