我正在ASP.NET Core MVC中创建一个基本的CRUD应用程序,允许用户与许多电视频道进行交互。当用户点击"编辑"按钮用于特定频道,此代码运行:
<a asp-action="Edit" asp-route-id="@Html.Raw(channel.ID)">Edit</a>
接下来,用户将被带到&#34;编辑&#34;视图。此视图包含具有每个可编辑属性的表单。以下是&#34;描述&#34;的示例。字段:
<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>
此字段应包含现有描述,以便用户不必记住它是什么。如何从&#34;索引&#34;中传递此信息?查看这个?
修改
根据请求,这是我的完整控制器:
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();
}
}
}
}
答案 0 :(得分:0)
要做到这一点,我必须改变两件事。
首先,我必须将每个参数添加到我的GET Edit方法中,并将这些参数存储在ViewData数组中:
// GET: Channel/Edit/5
public ActionResult Edit(long projID, string desc, string name, bool captureDiag, int nonNormalizedFloatHandling, long id, int writeOptimDutyCycle, int writeOptimMethod, string driver, bool itemPersistence, string itemPersistenceFile)
{
ViewData["projID"] = projID;
ViewData["desc"] = desc;
ViewData["name"] = name;
ViewData["captureDiag"] = captureDiag;
ViewData["nonNormalizedFloatHandling"] = nonNormalizedFloatHandling;
ViewData["id"] = id;
ViewData["writeOptimDutyCycle"] = writeOptimDutyCycle;
ViewData["writeOptimMethod"] = writeOptimMethod;
ViewData["driver"] = driver;
ViewData["itemPersistence"] = itemPersistence;
ViewData["itemPersistenceFile"] = itemPersistenceFile;
return View();
}
接下来,我必须在“索引”视图的“编辑”链接中添加其他asp-route-*
属性:
<a asp-action="Edit" asp-controller="Channel" asp-route-projid="@Html.Raw(channel.ProjID)" asp-route-desc="@Html.Raw(channel.Desc)" asp-route-name="@Html.Raw(channel.Name)" asp-route-capturediag="@Html.Raw(channel.CaptureDiag)" asp-route-nonnormalizedfloathandling="@Html.Raw(channel.NonNormalizedFloatHandling)" asp-route-id="@Html.Raw(channel.ID)" asp-route-writeoptimdutycycle="@Html.Raw(channel.WriteOptimDutyCycle)" asp-route-writeoptimmethod="@Html.Raw(channel.WriteOptimMethod)" asp-route-driver="@Html.Raw(channel.Driver)" asp-route-itempersistence="@Html.Raw(channel.ItemPersistence)" asp-route-itempersistencefile="@Html.Raw(channel.ItemPersistenceFile)">Edit</a> |
通过调用@ViewData["<PROP>"]
以及将asp-for="PROP"
标记添加到HTML元素,我可以通过执行这两项操作来正常访问每个媒体资源。
答案 1 :(得分:0)
您可以将模型传递给视图,而不是使用ViewData字典。
return View(model);
在您的视图中,设置模型:
@model YourModelType
在您看来,您现在可以使用@Model.Property