简体   繁体   English

单击mvc中的链接时,将相应的数据从一个视图传递到另一个视图

[英]Passing the corresponding data from one view to another on clicking link in mvc

I am stuck with how to pass data from one view to another. 我对如何将数据从一个视图传递到另一个视图感到困惑。 I have a view named " Session" which displays data from db through foreach loop in table format. 我有一个名为“会话”的视图,该视图以表格式通过foreach循环显示db中的数据。 Each row has a "add trainee" link. 每行都有一个“添加受训者”链接。 on its click it directs to another view. 单击它会转到另一个视图。 I need to get all the details from the row of link to get displayed on redirected view. 我需要从链接的行中获取所有详细信息,以在重定向视图上显示。

I dont have any clue on how to implement it neither have any sample code. 我对如何实现它没有任何线索,也没有任何示例代码。

Kindly help me with this. 请帮助我。

Actually there are couple of approaches to do this. 实际上,有两种方法可以做到这一点。

  1. pass the id to new view and get the detail data from database based on this id ,then display the detail. 将id传递到新视图,并基于此id从数据库获取详细信息数据,然后显示详细信息。
  2. get all the details from the row by javascript and store them in sessionstorage, then get this sessionstorage in another view and display all the detail. 通过javascript从该行中获取所有详细信息,并将其存储在sessionstorage中,然后在另一个视图中获取此sessionstorage并显示所有详细信息。

In your case, you should use query string <a href="/add-trainee?sessionid=123">Add trainee</a> Add trainee 对于您的情况,您应该使用查询字符串<a href="/add-trainee?sessionid=123">Add trainee</a> 添加受训者

while /add-trainee route to your TraineeController AddTrainee method 而/ add-trainee路由到TraineeController AddTrainee方法

I add sample code 我添加示例代码

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();
            }
        }


    }
}

Session Index (Index.cshtml) 会话索引(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 Trainee view (Add.cshtml) 添加学员视图(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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM