C# 如何将输入数据保存到模型中,然后将其存储在会话中,以便以后使用MVC进行显示?
我目前正在进行一个项目,该项目要求我保存用户输入的他/她的递送地址数据,并将其保存到会话中。我需要将数据存储在会话中,因为稍后我需要在我的感谢页面中显示这些数据 这是我的模型C# 如何将输入数据保存到模型中,然后将其存储在会话中,以便以后使用MVC进行显示?,c#,asp.net-mvc,C#,Asp.net Mvc,我目前正在进行一个项目,该项目要求我保存用户输入的他/她的递送地址数据,并将其保存到会话中。我需要将数据存储在会话中,因为稍后我需要在我的感谢页面中显示这些数据 这是我的模型 public class DeliveryAddressModel { [Required(ErrorMessage = "Please enter a name")] public string Name { get; set; } [Required(ErrorMessage = "Pleas
public class DeliveryAddressModel
{
[Required(ErrorMessage = "Please enter a name")]
public string Name { get; set; }
[Required(ErrorMessage = "Please enter the first address line")]
[Display(Name = "Line 1")]
public string Line1 { get; set; }
[Display(Name = "Line 2")]
public string Line2 { get; set; }
[Display(Name = "Line 3")]
public string Line3 { get; set; }
[Required(ErrorMessage = "Please enter a city name")]
public string City { get; set; }
[Required(ErrorMessage = "Please enter a state name")]
public string State { get; set; }
public string Zip { get; set; }
[Required(ErrorMessage = "Please enter a country name")]
public string Country { get; set; }
这是我的看法
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>DeliveryAddressModel</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<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">
@Html.LabelFor(model => model.Line1, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Line1, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Line1, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Line2, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Line2, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Line2, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Line3, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Line3, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Line3, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.City, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.City, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.City, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.State, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.State, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.State, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Zip, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Zip, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Zip, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Country, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Country, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Country, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.GiftWrap, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
@Html.EditorFor(model => model.GiftWrap)
@Html.ValidationMessageFor(model => model.GiftWrap, "", new { @class = "text-danger" })
</div>
</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>
}
我需要如何使我的控制器存储数据,以便以后使用?如果有其他更简单的方法,请告诉我。我是MVC新手 根据上面的查看结果初始化DeliveryAddressModel对象
[HttpPost]
public ActionResult Index(DeliveryAddressModel dam)
{
//Set this to the results sent back from your controller.
Session["Name"] = dam.Name();
Session["Line1"] = dam.Line1();
Session["Line2"] = dam.Line2();
Session["Line3"] = dam.Line3();
.
.
.
Session["Country "] = dam.Country();
return View();
}
然后,您可以访问会话对象,直到用户关闭其浏览器
在感谢页面视图中,您可以通过以下方式访问会话:
@Session["Country"]
你能使用JavaScript并将其保存在localStotage中吗?只要我能在“谢谢”页面上显示送货地址,一切都会好的。所以。。。是否将其保存在会话中?你试过什么?这对我来说是新的尝试。在上一次尝试中,我使用了一个SQL数据库模型,其中的值被提交,但我遇到了一些问题,即我的会话没有被填充。所以我做了研究,我得到的大部分东西是,我不需要SQL中的表,所以这是我迄今为止尝试过的。只需要帮助保存用户输入的数据,供以后使用,因为我想在另一个页面上显示用户输入的数据。所有这些都将在控制器中,对吗?控制器方法是否为[httppost]?当单击submit时,将数据添加到模型中的操作会是什么样子?谢谢你的回答是的,这在控制器里。我为此添加了伪代码。您的视图需要将数据发布到控制器。它们可以具有相同的方法名,例如“UserSignUp”。你可以发布你的控制器代码,让它更简单。我添加了我的控制器。我一直在思考下一步该怎么做。我的意思是,我不确定您是在尝试将数据持久化到数据库中,还是只是在尝试会话。通过我发布的更新,您可以访问用户提交的数据。将@Session[“Country”]放在视图中的某个位置,看看它是如何工作的。对于我在上面发布的控制器,当我进入@Session[“Country”]时,视图中不会显示任何内容。我只想让用户输入的数据一直存在,直到他退出。不想把它放到数据库里。
@Session["Country"]