【问题标题】:MVC: Submitting a view to itself - avoid model property values in return URLMVC:向自身提交视图 - 避免返回 URL 中的模型属性值
【发布时间】:2018-04-11 19:01:48
【问题描述】:

我是 MVC 的新手,我想创建一个简单的视图,允许用户输入一些数据 => 提交表单 => 在服务器上执行一些计算 => 并用计算的数据发回一个模型到同一个观点。 我找到了一种让它工作的方法,但我不确定这是否是最好的方法,让我困扰的是提交后,模型属性中的所有数据都在浏览器地址栏中可见: http://localhost:53718/?Number=4&Result=16

这是一个非常简化的场景的代码:

模型:

public class CalculationModel
{        
    public int Number { get; set; }        
    public int Result { get; set; }
}

控制者:

public class HomeController : Controller
{
    public ActionResult Index(CalculationModel model)
    {
        return View(model);
    }

    [HttpPost]
    public ActionResult Calculate(CalculationModel model)
    {
        model.Result = model.Number * model.Number;
        return RedirectToAction("Index", "Home", model); 
    }
}

观点:

        @model WebApplication1.CalculationModel

    @{
        ViewBag.Title = "Home Page";
    }

    <h2>Simple Calculation</h2>

    @using (Html.BeginForm("Calculate", "Home", FormMethod.Post)){

        <div class="form-horizontal" data-crypto-conversion-form>
            <div class="form-group">
                <div class="input-group">
                    @Html.EditorFor(model => model.Number, new { htmlAttributes = new { @class = "form-control " } })
                </div>
            </div>

            <div class="form-group">
                <div class="input-group">
                    @Html.EditorFor(model => model.Result, new { htmlAttributes = new { @class = "form-control " } })
                </div>
            </div>

            <div class="form-group">
                <input type="submit" value="Convert" class="btn btn-primary btn-sm submitConversion" />
            </div>

        </div>
    }

我不确定从 HttpPost 方法执行 RedirectToAction 是否是个好主意:return RedirectToAction("Index", "Home", model); - 有没有更好的方法?

有什么办法可以防止模型属性值显示在url/浏览器地址栏中?

非常感谢您的帮助。

【问题讨论】:

  • 您是否尝试过return Index(model); 作为替代方案?
  • 你可以return View("Index", "Home", model),或者干脆return View("Index", model)。

标签: c# asp.net-mvc razor


【解决方案1】:

RedirectToAction 方法基本上返回一个 302 响应,其中包含位置标头中的新 url,浏览器将向它发出一个新的 GET 请求。当您传递一个简单的模型时,它将被转换为查询字符串值,并将添加到位置标头中的新 url。

如果您不想查看查询字符串中的值,您可以使用 HttpPost 操作方法中的相同视图模型对象返回索引视图。确保清除模型状态字典,以便呈现 Result 属性的新值。您可以使用ModelState.Clear() 方法这样做。

[HttpPost]
public ActionResult Calculate(CalculationModel model)
{
    model.Result = model.Number * model.Number;
    ModelState.Clear();
    return View("Index", model); 
}

如果您仍想返回 A RedirectResult 响应,您可以这样做,但通过 TempData 传递模型数据。看看这个帖子

How do I include a model with a RedirectToAction?

【讨论】:

  • 哇,这速度太快了——我印象深刻。非常感谢,这正是我想要的。
【解决方案2】:

我会更改整体方法并使用相同的视图,因为您似乎需要一遍又一遍地计算。 Get Index 方法仅用于显示要处理的数据和/或结果,然后使用 Post 方法进行实际处理。

所以,控制器:

[HttpGet]
public ActionResult Index()
{
    return View();
}

[HttpPost]
public ActionResult Index(CalculationModel model)
{
    model.Result = model.Number * model.Number;
    model.Processed = true;
    return View(model);
}

型号

public class CalculationModel
{
    public int Number { get; set; }
    public int Result { get; set; }
    public bool Processed { get; set; }
    public CalculationModel()
    {
        Processed = false;
    }
}

还有视图,它只会在发布后显示结果。

@model MvcApplication2.Models.CalculationModel
@{
    ViewBag.Title = "Home Page";
}
<h2>Simple Calculation</h2>
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
 <div class="form-horizontal" data-crypto-conversion-form>
        <div class="form-group">
            <div class="input-group">
                @Html.EditorFor(model => model.Number, new { htmlAttributes = new { @class = "form-control " } })
            </div>
        </div>
        @*@(Model.Result)*@
        <div class="form-group">
            <div class="input-group">
                @*@Html.EditorFor(model => model.Result, new { htmlAttributes = new { @class = "form-control " } })*@
                @{if (Model!=null && Model.Processed)
                  {
                    <p>@(Model.Result)</p>
                  }
                }
            </div>
        </div>

        <div class="form-group">
            <input type="submit" value="Convert" class="btn btn-primary btn-sm submitConversion" />
        </div>
    </div>
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-11
    • 1970-01-01
    • 1970-01-01
    • 2014-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-26
    相关资源
    最近更新 更多