【问题标题】:Pass data between several views in MVC applications在 MVC 应用程序中的多个视图之间传递数据
【发布时间】:2017-05-12 03:39:48
【问题描述】:

我正在创建一个 MVC 应用程序,我想在视图之间传递数据。
这是我的第一个观点:

@model ClassDeclarationsThsesis.Models.AddGroupViewModel

@{
    ViewBag.Title = "Add Groups";
}

<h2>Add Groups to subjects</h2>

@foreach (var user in Model.Users)
{
    if (user.email.Replace(" ", String.Empty) == HttpContext.Current.User.Identity.Name)
    {
        if (user.user_type.Replace(" ", String.Empty) == 3.ToString() || user.user_type.Replace(" ", String.Empty) == 2.ToString())
        {
            using (Html.BeginForm("AddGroup", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
            {
                @Html.AntiForgeryToken()
                <h4>Create new groups.</h4>
                <hr />
                @Html.ValidationSummary("", new { @class = "text-danger" })
                <div class="form-group">
                    @{
                        List<SelectListItem> listItems1 = new List<SelectListItem>();

                    }
                    @foreach (var subject in Model.Subjects)
                    {
                        listItems1.Add(new SelectListItem
                        {
                            Text = subject.name,
                            Value = subject.name,
                            Selected = true
                        });
                    }
                    @Html.LabelFor(m => m.subject_name, new { @class = "col-md-2 control-label" })
                    <div class="col-md-10">
                        @Html.DropDownListFor(m => m.subject_name, listItems1, new { @class = "form-control" })
                    </div>
                </div>
                <div class="form-group">
                    @Html.LabelFor(m => m.qty, new { @class = "col-md-2 control-label" })
                    <div class="col-md-10">
                        @Html.TextBoxFor(m => m.qty, new { @class = "form-control" })
                    </div>
                </div>


                <div class="form-group">
                    <div class="col-md-offset-2 col-md-10">
                        <input type="submit" class="btn btn-default" value="Submit" />
                    </div>
                </div>

                            }
                        }
                        if (user.user_type.Replace(" ", String.Empty) == 1.ToString())
                        {
                            <p>You do not have enough permissions to enter this page. Contact the administrator.</p>
                                }

                            }
                        }

还有我的控制器:

public ActionResult AddGroup(AddGroupViewModel model)
{
    var entities = new ClassDeclarationsDBEntities1();
    var model1 = new AddGroupViewModel();
    model1.Subjects = entities.Subjects.ToList();
    model1.Users = entities.Users.ToList();
    // set your other properties too?


    if (ModelState.IsValid)
    {

        return RedirectToAction("AddGroupsQty", "Account");
    }
    return View(model1);
}

我想要实现的是将下拉列表中的选定项目和此数量变量传递给 AddGroupsQty 视图。我该怎么做呢?在我的 AddGroupsQty 控制器中,到目前为止,我只有一个简单的视图返回。

【问题讨论】:

  • RouteConfig.cs 中是否有通往新视图的路径?或者您是否尝试根据输入的更改值重新显示当前视图?
  • @Mmcgowa3 不,我没有在那里添加任何路线。我只是想根据该视图的输入显示不同的视图。

标签: c# asp.net .net asp.net-mvc model-view-controller


【解决方案1】:

您可以使用查询字符串传递值。

return RedirectToAction("AddGroupsQty", "Account", 
                                         new { qty=model.qty,subject=model.subject_name);

假设你的AddGroupsQty有2个参数来接受数量和主题

public ActionResult AddGroupsQty(int qty,string subject)
{
  // do something with the parameter
  // to do : return something
}

这将使浏览器使用查询字符串中的值发出新的 GET 请求。如果你不喜欢这样做,你可以使用像 TempData 这样的服务器端临时持久机制

TempData["qty"]=model.qty;
TempData["subject"]= model.subject_name;
return RedirectToAction("AddGroupsQty", "Account");

在您的AddGroupsQty 操作中,

public ActionResult AddGroupsQty()
{
  int qty=0;
  string subjectName=string.Empty;
  if(TempData["qty"]!=null)
  {
    qty = Convert.ToInt32(TempData["qty"]);
  }
  if(TempData["subject"]!=null)
  {
    subjectName = TempData["subject"];
  }
  // Use this as needed
  return View();
}

如果您想将这些值从 ADdGroupsQty 操作传递到它的视图,您可以使用视图模型或 ViewBag/ViewData。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多