如何将输入数据保存到模型中,然后将其存储在会话中以供以后使用MVC显示?

时间:2018-08-23 11:59:17

标签: c# asp.net-mvc

我目前正在从事一个项目,该项目需要我保存用户输入的送货地址数据,并将其保存到会话中。我需要将数据存储在会话中,因为稍后需要在“谢谢”页面中显示此数据。

这是我的模特

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>
}

我的控制器

public class DeliveryAddressController : Controller
{
    // GET: DeliveryAddress
    public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Address(DeliveryAddressModel dam)
    {


        Session["Name"] = dam.Name;
        Session["Line1"] = dam.Line1;
        Session["Line2"] = dam.Line2;
        Session["Line3"] = dam.Line3;
        Session["City"] = dam.City;
        Session["State"] = dam.State;
        Session["Zip"] = dam.Zip;
        Session["Country "] = dam.Country;

        return View(dam);
    }

}

我将如何使我的控制器存储数据以供以后使用?如果还有其他更简单的方法,请告诉我。我是MVC的新手。

1 个答案:

答案 0 :(得分:0)

根据您在那里的“查看”结果初始化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对象,直到用户关闭其浏览器。

在“谢谢”页面视图中,您可以通过以下方式访问会话:

@Session["Country"]