首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >在不进行编辑的情况下通过视图将模型传递给另一个操作

在不进行编辑的情况下通过视图将模型传递给另一个操作
EN

Stack Overflow用户
提问于 2015-07-17 06:35:30
回答 1查看 68关注 0票数 0

我有一个接受大量数据的表单。所有数据都存储在一个模型对象中。

当提交表单时,如果通过验证,模型将传递给确认视图,该视图仅显示表单中提交的所有信息。

代码语言:javascript
代码运行次数:0
运行
复制
[HttpPost]
    public ActionResult ClientProfile(ClientProfileFormModel model)
    {
        if (ModelState.IsValid)
        {
            return View("ClientProfileConfirmation",model);
            }

        return View(model);
    }

当用户单击底部的提交按钮时,我需要模型进入一个操作,以便我可以发送电子邮件。

代码语言:javascript
代码运行次数:0
运行
复制
[HttpPost]
    public ActionResult ClientProfileConfirmationSubmit(ClientProfileFormModel model)
    {

        string emailToAdminBody = GenerateFirmProfileAdminEmail(model);
        EmailLogic.Instance.SendEmail("test@test.com", "test@test.com", "Firm profile from " + model.FirmAddress.Name, emailToAdminBody);

        return View(model);
    }

我的问题是:我需要一种简单的方法来获取我的模型,从表单的HttpPost操作(它在验证后将您发送到确认页面)到确认的HttpPost操作。我希望避免用隐藏的输入填充确认视图,这样我就可以通过表单将其全部带入。

我也尝试过将模型存储在Session和TempData中,但由于某些原因,这两种方法都返回null。我认为这与经历多个动作有关。

这真的不应该那么难!我遗漏了什么?将一堆隐藏的输入字段放入确认页的表单中是唯一的方法吗?

EN

回答 1

Stack Overflow用户

发布于 2015-07-17 09:05:34

我用过TempData,它工作得很好。

模型:

代码语言:javascript
代码运行次数:0
运行
复制
public class ClientProfileFormModel
{
    public string Name { get; set; }
    public string Address { get; set; }
    public string Country { get; set; }
}

控制器:

代码语言:javascript
代码运行次数:0
运行
复制
public class ClientController : Controller
{
    // GET
    public ActionResult ClientProfile()
    {
        return View();
    }

    [HttpPost]
    public ActionResult ClientProfile(ClientProfileFormModel model)
    {
        if (ModelState.IsValid)
        {
            return RedirectToAction("ClientProfileConfirmation", model);
        }

        return View(model);
    }

    // GET
    public ActionResult ClientProfileConfirmation(ClientProfileFormModel model)
    {
        return View(model);
    }

    [HttpPost]
    public ActionResult ClientProfileConfirmation()
    {
        var model = (ClientProfileFormModel) TempData["clientProfile"];
        string emailToAdminBody = GenerateFirmProfileAdminEmail(model);
        EmailLogic.Instance.SendEmail(...);

        TempData["success-message"] = "Your profile has been approved. Check your inbox.";
        return View("ClientProfileConfirmation", model);
    }
}

ClientProfile视图

代码语言:javascript
代码运行次数:0
运行
复制
@model StackOverflow.Models._31465719.ClientProfileFormModel

@{
    ViewBag.Title = "ClientProfile";
}

<h2>ClientProfile</h2>

@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>ClientProfileFormModel</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.Address, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Address, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Address, "", 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">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

ClientProfile确认视图:

代码语言:javascript
代码运行次数:0
运行
复制
@model StackOverflow.Models._31465719.ClientProfileFormModel

@{
    TempData["clientProfile"] = Model;
    ViewBag.Title = "ClientProfileConfirmation";
    var successMessage = TempData["success-message"];
}

<h2>ClientProfileConfirmation</h2>

@if (successMessage != null)
{
    <div class="alert alert-success">
        <p class="text-success">@successMessage</p>
    </div>
}

@using (Html.BeginForm())
{
    <div>
        <h4>ClientProfileFormModel</h4>
        <hr />
        <dl class="dl-horizontal">
            <dt>
                @Html.DisplayNameFor(model => model.Name)
            </dt>

            <dd>
                @Html.DisplayFor(model => model.Name)
            </dd>

            <dt>
                @Html.DisplayNameFor(model => model.Address)
            </dt>

            <dd>
                @Html.DisplayFor(model => model.Address)
            </dd>

            <dt>
                @Html.DisplayNameFor(model => model.Country)
            </dt>

            <dd>
                @Html.DisplayFor(model => model.Country)
            </dd>

        </dl>
    </div>

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

我在最后一个确认视图中所做的就是将模型保存到TempData中:

代码语言:javascript
代码运行次数:0
运行
复制
TempData["clientProfile"] = Model;

然后在控制器中读取它:

代码语言:javascript
代码运行次数:0
运行
复制
var model = (ClientProfileFormModel) TempData["clientProfile"];
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31465719

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档