![](/img/trans.png)
[英]How to display a specific MVC Partial View when clicking a corresponding link?
[英]Passing the corresponding data from one view to another on clicking link in mvc
我對如何將數據從一個視圖傳遞到另一個視圖感到困惑。 我有一個名為“會話”的視圖,該視圖以表格式通過foreach循環顯示db中的數據。 每行都有一個“添加受訓者”鏈接。 單擊它會轉到另一個視圖。 我需要從鏈接的行中獲取所有詳細信息,以在重定向視圖上顯示。
我對如何實現它沒有任何線索,也沒有任何示例代碼。
請幫助我。
實際上,有兩種方法可以做到這一點。
對於您的情況,您應該使用查詢字符串<a href="/add-trainee?sessionid=123">Add trainee</a>
添加受訓者
而/ add-trainee路由到TraineeController AddTrainee方法
我添加示例代碼
namespace WebApplication1.Controllers
{
public class Session
{
public int SessionId { get; set; }
public string Name { get; set; }
public List<Trainee> Trainees { get; set; }
}
public class Trainee
{
public int TraineeId { get; set; }
public string Name { get; set; }
}
public class SessionController : Controller
{
// GET: Session
public ActionResult Index()
{
List<Session> sessions = new List<Session>();
sessions.Add(new Session { SessionId = 1, Name = "Session 1"});
sessions.Add(new Session { SessionId = 1, Name = "Session 2" });
sessions.Add(new Session { SessionId = 1, Name = "Session 3" });
return View(sessions);
}
}
}
namespace WebApplication1.Controllers
{
public class TraineeController : Controller
{
public ActionResult Add(int sessionid)
{
List<Session> sessions = new List<Session>();
sessions.Add(new Session { SessionId = 1, Name = "Session 1" });
sessions.Add(new Session { SessionId = 1, Name = "Session 2" });
sessions.Add(new Session { SessionId = 1, Name = "Session 3" });
var session = sessions.FirstOrDefault(c => c.SessionId == sessionid);
ViewBag.session = session;
return View();
}
[HttpPost]
public ActionResult Create(FormCollection collection)
{
try
{
// TODO: Add insert logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
}
}
會話索引(Index.cshtml)
@model IEnumerable<WebApplication1.Controllers.Session>
@{
ViewBag.Title = "List Sessions";
}
<table>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
<a href="/trainee/add?sessionid=@item.SessionId">Add Trainee</a>
</td>
</tr>
}
</table>
添加學員視圖(Add.cshtml)
@model WebApplication1.Controllers.Trainee
@{
var session = ViewBag.session;
}
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Add Trainee</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
<label class="control-label col-md-2" for="Name">Session Name</label>
<div class="col-md-10">@session.Name</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.