如何从不属于任何模型的表单传递输入值?

时间:2014-09-10 17:51:44

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

在剃刀视图中,我有一个表单:

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

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

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

等。很长一段时间,但我想将下面这些值传递给控制器​​:

            <div class="form-group">
                <input class="form-control text-box single-line valid" id="FetchDate" name="FetchDate" type="date">
                <input class="form-control text-box single-line valid" id="FetchTime" name="FetchTime" type="time">
            </div>


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

Submit(type="submit)按钮在POST控制器中调用Person编辑操作:

[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit([Bind(Include = "Id,FirstName")] Person person) {
        if (ModelState.IsValid) {
            db.Entry(person).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(person);
    }

我们可以看到唯一的参数是Person person,但在表单中我添加了两个与Person没有任何共同点的字段,但我也想将值从它们传递给POST Edit按提交按钮:

<div class="form-group">
    <label class="control-label col-md-2" >If you want this client to be fetched to you later, fullfil the data.</label>
    <input class="form-control text-box single-line valid" id="FetchDate" name="FetchDate" type="date">
    <input class="form-control text-box single-line valid" id="FetchTime" name="FetchTime" type="time">
</div>

问题:如何将值传递给控制器​​的POST Edit操作,这些操作不属于我的Razor视图中的任何模型(上图)?


我试过这个,时间为空或空字符串(还没有检查):

[HttpPost]

[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "Id,FirstName")] Person person, [Bind(Include="FetchTime")] String time) {
    Debug.WriteLine("Time " + time);
    if (ModelState.IsValid) {
        db.Entry(person).State = EntityState.Modified;
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    return View(person);
}

1 个答案:

答案 0 :(得分:1)

你可以这样做:

public ActionResult Edit([Bind(Include = "Id,FirstName")] Person person, string FetchDate, string FetchTime) {
    if (ModelState.IsValid) {
        db.Entry(person).State = EntityState.Modified;
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    return View(person);
}