简体   繁体   中英

How to pass input value from the form which is not part of any model?

In razor view I have a 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>

etc. for long time,but I want to pass values from these below to controller:

            <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) button invokes POST edit action in Person controller:

[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);
    }

We can see that the only parameter is Person person , but inside the form I added two fields which have nothing in common with a Person but I also would like to pass values from them to POST Edit by submit button:

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

Question: How to pass values to controller's POST Edit action which are not part of any model from my Razor view(above)?


I tried this and time is null or empty string(didn't check yet):

[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);
}

you can do this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM