c# - How to pass input value from the form which is not part of any model? -
in razor view have 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. long time,but want pass values these below 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 parameter person person
, inside form added 2 fields have nothing in common person pass values them post edit
submit button:
<div class="form-group"> <label class="control-label col-md-2" >if want client fetched later, fullfil 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 pass values controller's post edit
action not part of model razor view(above)?
i tried , time 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 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); }
Comments
Post a Comment