【问题标题】:MVC C# Session ProjectMVC C# 会话项目
【发布时间】:2016-07-16 03:13:51
【问题描述】:

我已经发布了这个问题,但没有人回答我遇到的 cmets/错误。如何使用会话变量在我的索引视图中获取在 @Html.TextBox("SearchString") 中输入的文本以欢迎视图?

HomeController.Cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.Entity;
using Newproject.Models;

namespace Newproject.Controllers
{
 public class HomeController : Controller
  {
    public ActionResult Index()
    {
        return View();
    }
    [HttpPost]
    public ActionResult Gender(WelcomeVm model)

    {
        model.Genders = new List<SelectListItem>
   {
     new SelectListItem { Value="Male", Text="Male"},
     new SelectListItem { Value="Female", Text="Female"}
    };
        return View(model);
    }
    [HttpPost]
    public ActionResult Welcome(WelcomeVm model)
     {
       return View(model);
     }
    public object Final { get; set; }
 }
 }

索引.cshtml

@{
ViewBag.Title = "Welcome";
}

<br />
@Html.Label("Name")
<br />
@using (Html.BeginForm("Gender", "Home", FormMethod.Post))
{
@Html.TextBox("Name");
<input id="btnSubmit" name="btnSubmit" placeholder="test" type="submit" value="Submit" />
}

性别.cs

using System.Collections.Generic;
using System.Web.Mvc;
public class WelcomeVm
{
public string Name { set; get; }
public string Gender { set; get; }
public IEnumerable<SelectListItem> Genders { set; get; }
}

性别.cshtml

@model WelcomeVm
@using (Html.BeginForm("Welcome", "Home", FormMethod.Post))
{
<p>Hello @Model.Name what is your gender</p>
@Html.DropDownListFor(m => m.Gender, Model.Genders)
@Html.HiddenFor(s => s.Name)
<input type="submit" />
}

Welcome.cshtml

@{
ViewBag.Title = "Welcome";
}

@model WelcomeVm
<p>Hello @Model.Name who is a @Model.Gender</p>

【问题讨论】:

  • 如果你没有得到答案,那么再问同样的问题不是正确的方法。而是改进/编辑您现有的问题,添加更多信息以帮助人们回答它。
  • 请将您的问题的标题更新为...一个实际问题。只说技术关键词很可能会减少您的访问量。

标签: c# asp.net asp.net-mvc session


【解决方案1】:

您不需要 Session 或 ViewBag 来传递数据。您可以简单地使用现有的视图模型。

所以在你的索引视图中,

@Html.Label("Name")  
@using (Html.BeginForm("Gender", "Home"))
{
  @Html.TextBox("Name");
  <input id="btnSubmit" name="btnSubmit" type="submit"    value="Submit" />
}

并且在您的Gender 操作方法中使用与您的参数类型相同的视图模型。当表单发布时,默认模型绑定器会将发布的表单值映射到 WelcomeVm 类的对象。

[HttpPost]
public ActionResult Gender(WelcomeVm model)
{
   model.Genders = new List<SelectListItem>
   {
         new SelectListItem { Value="Male", Text="Male"},
         new SelectListItem { Value="Female", Text="Female"}
   };
   return View(model);
}

Gender.cshtml 视图中,该视图再次被强类型化到同一视图模型。

@model WelcomeVm
@using (Html.BeginForm("Welcome", "Home", FormMethod.Post))
{
  <p>Hello @Model.Name what is your gender</p>
  @Html.DropDownListFor(m => m.Gender, Model.Genders)
  @Html.HiddenFor(s=>s.Name)
  <input type="submit" />
}

在欢迎操作中

[HttpPost]
public ActionResult Welcome(WelcomeVm model)
{
  return View(model);
}

在 Welcome.cshtml 视图中,它再次被强类型化到您的视图模型中,

@model WelcomeVm
<p>Hello @SearchString who is a @Model.Gender</p>

【讨论】:

  • 在我的 Gender.cshtml 页面中,它给了我一个错误。 @Html.DropDownListFor(m => m.Gender, Model.Genders) 说它的 InvalidOperationException 未被使用的代码处理
  • 我按照你所说的 Shyju 做了所有事情,但是在索引页文本页中输入的名称没有显示在欢迎页中
  • 对于您的第一个问题,您是否在 Gener 操作方法中加载 Genders 集合?对于第二条评论,您是否在第二个视图中看到名称(性别)?
  • 是的,我在性别视图中看到了这个名字
  • 我的第一个问题错误已修复不再出现
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-06
相关资源
最近更新 更多