[英]Two Viewbags with same name,value pair in one View returning the 0 value to the controller?
I have two viewbags which I am binding with Dropdownlist these two viewbags have same name, value pair when I post back the form and retrieving the data using model then there is 0 value in the model.我有两个视图包,我用 Dropdownlist 绑定这两个视图包具有相同的名称、值对,当我回发表单并使用 model 检索数据时,model 中的值为 0。 I have two div and I want to submit one at a time because I am selecting the div by using the Is Cloud Totalizer?
我有两个 div,我想一次提交一个,因为我使用 Is Cloud Totalizer 选择 div? checkbox.
复选框。 What is the problem?
问题是什么?
[HttpGet]
public ActionResult CreateNewTotalizerTag()
{
//This is RawTag List
var RawTaglist1 = cRTu.RawTagsList();
ViewBag.RawTaglist1 = new SelectList(RawTaglist1, "Real_Tag_Id", "R_Tag_Name");
//This is RawTag List
var RawTaglist2 = cRTu.RawTagsList();
ViewBag.RawTaglist2 = new SelectList(RawTaglist2, "Real_Tag_Id", "R_Tag_Name");
return View();
}
[HttpPost]
public ActionResult CreateNewTotalizerTag(RawTagVM rawTagVM)
{
//This is RawTag List
var RawTaglist1 = cRTu.RawTagsList();
ViewBag.RawTaglist1 = new SelectList(RawTaglist1, "Real_Tag_Id", "R_Tag_Name");
//This is RawTag List
var RawTaglist2 = cRTu.RawTagsList();
ViewBag.RawTaglist2 = new SelectList(RawTaglist2, "Real_Tag_Id", "R_Tag_Name");
return RedirectToAction("TotalizerTagsList");
}
//View
@using (Html.BeginForm("CreateNewTotalizerTag", "TotalizerTags", FormMethod.Post))
{
@Html.HiddenFor(x=>x.Real_Tag_Id)
<div class="container-fluid">
<div class="row">
<div class="col-md-3">
</div>
<div class="col-md-6">
<!-- general form elements disabled -->
<div class="card card-warning">
<div class="card-header" style="background-color:#343a40">
<h3 class="card-title" style="color:white">Create Totalizer Tag</h3>
</div>
<!-- This form is to create Raw tag totalizer onPrem or onCloud -->
<div class="card-body">
<form role="form">
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<div class="custom-control custom-switch">
@Html.CheckBoxFor(m => m.Is_Cloud_Totalizer, new { @class = "custom-control-input", id = "Is_Cloud_Totalizer" })
<label class="custom-control-label" for="Is_Cloud_Totalizer">Is Cloud Totalizer?</label>
</div>
</div>
</div>
</div>
<!-- On Cloud Inputs starts here -->
<div id="oncloud_totalizer" style="display:none">
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<label>Enter Tag Name</label>
@Html.EditorFor(m => m.R_Tag_Name, new { htmlAttributes = new { @class = "form-control" } })
@*@Html.ValidationMessageFor(model => model.R_Tag_Name, "", new { @class = "text-danger" })*@
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label>Select Raw Tag</label>
@Html.DropDownListFor(m => m.Real_Tag_Id_Cloud, (IEnumerable<SelectListItem>)ViewBag.RawTaglist1, "Select Raw Tag", new { @class = "form-control", id = "Raw_Tag_List1",name="Raw_Tag_List1" })
</div>
</div>
</div>
</div>
<!-- On Prem Inputs starts here -->
<div class="row" id="onPrem_totalizer">
<div class="col-sm-6">
<div class="form-group">
<label>Select Raw Tag</label>
@Html.DropDownListFor(m => m.Real_Tag_Id, (IEnumerable<SelectListItem>)ViewBag.RawTaglist2, "Select Raw Tag", new { @class = "form-control", id = "Raw_Tag_List2",name="Raw_Tag_List2" })
</div>
</div>
</div>
<div class="form-group">
<center><button type="submit" id="submit" class="btn btn-primary">Create Totalizer</button></center>
</div>
</form>
</div>
<!-- /.card-body -->
</div>
<!-- /.card -->
<!-- general form elements disabled -->
<!-- /.card -->
</div>
</div>
</div>
}
<script>
$(document).ready(function () {
// Initialize select2
//$("#Raw_Tag_List1").select2();
//$("#Raw_Tag_List2").select2();
//$("#Source_Tag_List").select2();
$("#Is_Cloud_Totalizer").change(function () {
if (this.checked) {
$("#oncloud_totalizer").show();
$("#onPrem_totalizer").hide();
}
else {
$("#onPrem_totalizer").show();
$("#oncloud_totalizer").hide();
}
});
});
</script>
There is no point in putting anything in the ViewBag
if you then do a RedirectToAction
(or any kind of redirect).如果您随后执行
RedirectToAction
(或任何类型的重定向),则在ViewBag
中放置任何内容都没有意义。
ViewBag
is there for a controller to pass data to its own view. ViewBag
用于 controller 将数据传递到自己的视图。 Once a response is sent to the browser, the ViewBag
is lost, and a new one is created on the next request.一旦向浏览器发送响应,
ViewBag
就会丢失,并在下一个请求时创建一个新的。
A redirect sends a response to the browser, telling it to request the chosen endpoint.重定向向浏览器发送响应,告诉它请求选择的端点。
Thus any ViewBag
data set up before the return Redirect...
is lost.因此,在
return Redirect...
之前设置的任何ViewBag
数据都会丢失。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.