如何将查询字符串中的参数从视图传递给控制器

时间:2014-09-20 17:52:33

标签: c# html asp.net-mvc razor

Create视图中,我有以下链接:http://localhost:17697/Reports/Create?personId=2

在视图中我有一个表单:

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

    <div class="form-horizontal">

        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        @Html.HiddenFor(model => model.Id)

        <div class="form-group">
            @Html.LabelFor(model => model.Text, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Text, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Text, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-0 col-md-10">
                <input type="submit" value="Save" class="btn btn-success" style="width: 200px; font-weight: bold;" />
            </div>
        </div>
    </div>

当我点击Save按钮时,我提交表单并调用POST Create操作方法。我也会将查询字符串中2的{​​{1}}从视图中的链接传递给?personId=2方法。

POST Create

在表单中添加这个是正确的方法吗? [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create([Bind(Include = "Id,Text")] Report report) { // I WANT TO GET personId=2 from query string here if (ModelState.IsValid) { db.Reports.Add(report); db.SaveChanges(); return RedirectToAction("Index"); } return View(report); }

1 个答案:

答案 0 :(得分:2)

将其添加为参数...

public ActionResult Create([Bind(Include = "Id,Text")] Report report, int personId){
}