Answered by:
how to (pass) send form value(textbox and radio button) to controller==>(save $ show in new view)

Question
-
User1928394251 posted
my form:
@using (Html.BeginForm("save", "modiran", FormMethod.Post, null))
{
@Html.AntiForgeryToken()
// <label>id=</label>
// <input type="text" />
<label>name=</label>
<input type="text" />
<label>modirkol</label>
<input type="radio" name="semat" value="modirkol" asp-for="input.semat" />
<label>ghaemmagham</label>
<input type="radio" name="semat" value="moaven" asp-for="input.semat" />
<label>modir</label>
<button type="submit" value="insert" title="ذخیره"> ذخیره </button>
}=================my controller : i
public ActionResult save(FormCollection frm )
{
var model = new modiran();
var semat = Request.Form["semat"];
// var frm=db.modiran.
TryUpdateModel(model, frm);
TempData["modiran_id"] = model.modiran_id;
TempData["modiran_name"] = model.modiran_name;
// if (semat==0) then
return View();
}my new view:
<div class="form-horizontal">
<div class="panel panel-body">
<div style="direction:rtl !important;margin:40px auto;width:70%">
<div class="form-group">نام :
<div class="col-md-10">
@Html.TextBox("modiran_name", (string)ViewBag.modiran_name, new { @style = "width: 300px;" })
</div>
</div>
<div class="form-group">سمت :
<div class="col-md-10">
@Html.TextBox("modiran_semat", (string)ViewBag.modiran_semat, new { @style = "width: 300px;" })
</div>
</div>>
</div>
</div>help me please
my new view: ???????i
Monday, December 24, 2018 3:29 PM
Answers
-
User-474980206 posted
the browser posts back enabled named form elements as name/value pairs. in the case of checkbox or radio, only the selected values are posted. your form only has 2 named elements, which are the radio buttons named seat. so the line:
var semat = Request.Form["semat"];
the variable will set to the postbox model. you don't show the model, but if it has a property named "semat", the line:
TryUpdateModel(model, frm);will be set, but all other properties will be null, or the default value for value types. as neither "modiran_id" nor "modiran_name" are included in the post data, the first will be zero (if a number) and the second will be null.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, December 24, 2018 4:57 PM
All replies
-
User-474980206 posted
the browser posts back enabled named form elements as name/value pairs. in the case of checkbox or radio, only the selected values are posted. your form only has 2 named elements, which are the radio buttons named seat. so the line:
var semat = Request.Form["semat"];
the variable will set to the postbox model. you don't show the model, but if it has a property named "semat", the line:
TryUpdateModel(model, frm);will be set, but all other properties will be null, or the default value for value types. as neither "modiran_id" nor "modiran_name" are included in the post data, the first will be zero (if a number) and the second will be null.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, December 24, 2018 4:57 PM -
User1928394251 posted
hello.thank you .
my view is :
@using (Html.BeginForm("save", "modiran", FormMethod.Post, null))
{
@Html.AntiForgeryToken()
// <label>id=</label>
// <input type="text" />
<label>name=</label>
<input type="text" />
<label>modirkol</label>
<input type="radio" name="semat" value="modirkol" asp-for="input.semat" />
<label>ghaemmagham</label>
<input type="radio" name="semat" value="moaven" asp-for="input.semat" />
<label>modir</label>
<button type="submit" value="insert" title="ذخیره"> ذخیره </button>
}i want to save in db and show in new view.
my controller is :
public ActionResult save([Bind(Include = "modiran_id,modiran_name,modiran_semat")] modiran model )
{
// var model = new modiran();
var semat = Request.Form["semat"];
// var frm=db.modiran.
TryUpdateModel(model, semat);
// TempData["modiran_id"] = model.modiran_id;
// TempData["modiran_name"] = model.modiran_name;
// if (semat==0) then
// TempData["modiran_semat"] = model.modiran_semat;
// TempData["modiran_semat"] = semat.Trim("modirkol");
return RedirectToAction("Index_savemodiran1");
// return View();
}in my new view feilds is empty and dont save in my table sorry .
please help me . thanks .
Tuesday, December 25, 2018 5:16 AM -
User453055610 posted
You can set Object in javacsript send controller
Tuesday, December 25, 2018 6:36 AM