nu123 nu123 - 1 month ago 5
C# Question

Multiple ViewModels in a single MVC 4 View asp.net didn't work as expected

I have PromotionAdmin controller and view for get data from user.I'm trying to get data from multiple forms in view I have two form in view like this

<div class="col-md-9 well admin-content" id="spotpromotion">
<div class="row setup-content" id="step-1">
<div class="col-xs-8 col-md-offset-2">
<div class="col-md-12">
@using (Html.BeginForm("SpotPromotion", "PromotionAdmin", FormMethod.Post, new { @class = "form-group", enctype = "multipart/form-data", id = "SpotsubmitForm" }))
{
@Html.AntiForgeryToken()
<div class="form-group">
<label class="control-label" for="idspchgpointtxt">Charge Point ID<span style="color: red">*</span> :</label>
<input class="form-control" name="spchgpointid" placeholder="Charge Point" id="spchgpointid" required="required">
</div>
<div class="form-group">
<label class="control-label col-sm-4" for="idspmessagetxt">Message<span style="color: red">*</span> :&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
<textarea class="form-control" id="spmessage" placeholder="Message" name="spmessage" rows="5" ></textarea>
</div>
<div class="form-group">
<label class="control-label col-sm-4" for="idspstarttimetxt">Start Time<span style="color: red">*</span> :</label>
<div class="row">
<div class='col-sm-12'>
<div class="form-group">
<div class='input-group date' id='datetimepicker1'>
<input class="date-picker form-control" placeholder="Start Time" id="spstarttimetxt" name="spstarttimetxt" />
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
</div>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-4" for="idspstarttimetxt">End Time<span style="color: red">*</span> :</label>
<div class="row">
<div class='col-sm-12'>
<div class="form-group">
<div class='input-group date' id='datetimepicker1'>
<input class="date-picker form-control" placeholder="End Time" id="spendtimetxt" name="spendtimetxt" />
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
</div>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-4" for="idsptargetcritxt">Target Criteria<span style="color: red">*</span> :</label>
<br />
</div>
<div style="margin:30px;">
<div class="form-group ">
<label class="control-label col-sm-4" for="idspstarttxt">Start</label>
<input type="radio" name="srptargetcriradio" id="spstartradio" onclick="document.getElementById('spvaluetxt').style.display = 'none';">
</div>
<div class="form-group ">
<label class="control-label col-sm-4" for="idspchagingtxt">Chaging</label>
<input type="radio" name="srptargetcriradio" id="spchagingradio" onclick="document.getElementById('spvaluetxt').style.display = 'none';">
</div>
<div class="form-group ">
<label class="control-label col-sm-4" for="idspendtxt">End</label>
<input type="radio" name="srptargetcriradio" id="spendradio" onclick="document.getElementById('spvaluetxt').style.display = 'none';">
</div>
<div class="form-group">
<label class="control-label col-sm-4" for="idsptargetcritxt">Value</label>
<input type="radio" name="srptargetcriradio" id="spvalueradio" onclick="document.getElementById('spvaluetxt').style.display = 'block';">
<input type="number" min="0" class="form-control" placeholder="Value" id="spvaluetxt" name="spvaluetxt" required="required" style="display:none;" />
</div>
</div>
<button class="btn btn-primary nextBtn btn-lg pull-right" name="spsubmit" id="spsubmit" type="button">Submit</button>
}
</div>
</div>
</div>
</div>

<div class="col-md-9 well admin-content" id="targetpromotion">
<div class="row setup-content" id="step-1">
<div class="col-xs-8 col-md-offset-2">
@using (Html.BeginForm("TargetPromotion", "PromotionAdmin", FormMethod.Post, new { @class = "form-group", enctype = "multipart/form-data", id = "TargetsubmitForm" }))
{
@Html.AntiForgeryToken()
<div class="col-md-12">
<div class="form-group">
<label class="control-label" for="idtpchgpointtxt">Charge Point ID<span style="color: red">*</span> :</label>
<input class="form-control" placeholder="Charge Point" id="tpchgpointid" name="tpchgpointid" required="required">
</div>
<div class="form-group">
<label class="control-label col-sm-4" for="idtpmessagetxt">Message<span style="color: red">*</span> :&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
<textarea class="form-control" id="spmessage" placeholder="Message" name="tpmessage" rows="5" required="required"></textarea>
</div>
<div class="form-group">
<label class="control-label col-sm-4" for="idtpstarttimetxt">Start Time<span style="color: red">*</span> :</label>
<div class="row">
<div class='col-sm-12'>
<div class="form-group">
<div class='input-group date' id='datetimepicker1'>
<input class="date-picker form-control" placeholder="Start Time" id="tpstarttimetxt" name="tpstarttimetxt" required="required" />
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
</div>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-4" for="idtpstarttimetxt">End Time<span style="color: red">*</span> :</label>
<div class="row">
<div class='col-sm-12'>
<div class="form-group">
<div class='input-group date' id='datetimepicker1'>
<input class="date-picker form-control" placeholder="End Time" id="tpendtimetxt" name="tpendtimetxt" required="required" />
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
</div>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-4" for="idtptargetcritxt">Target Criteria<span style="color: red">*</span> :</label>
<br />
</div>
<div style="margin:30px;">
<div class="form-group ">
<label class="control-label col-sm-4" for="idtpstarttxt">Start</label>
<input type="radio" name="srptargetcriradio" id="tpstartradio" onclick="document.getElementById('tpvaluetxt').style.display = 'none';">
</div>
<div class="form-group ">
<label class="control-label col-sm-4" for="idtpchagingtxt">Chaging</label>
<input type="radio" name="srptargetcriradio" id="tpchagingradio" onclick="document.getElementById('tpvaluetxt').style.display = 'none';">
</div>
<div class="form-group ">
<label class="control-label col-sm-4" for="idtpendtxt">End</label>
<input type="radio" name="srptargetcriradio" id="tpendradio" onclick="document.getElementById('tpvaluetxt').style.display = 'none';">
</div>
<div class="form-group">
<label class="control-label col-sm-4" for="idtpptargetcritxt">Value</label>
<input type="radio" name="srptargetcriradio" id="tpvalueradio" onclick="document.getElementById('tpvaluetxt').style.display = 'block';">
<input type="number" min="0" class="form-control" placeholder="Value" id="tpvaluetxt" name="tpvaluetxt" required="required" style="display:none;" />
</div>
</div>
<button class="btn btn-primary nextBtn btn-lg pull-right" name="tpsubmit" type="button">Submit</button>
</div>
}
</div>
</div>
</div>


I'm calling PromotionAdmin Controller I'm calling following two methods

[CustomAuthorize(Roles = "admin")]
[HttpPost]
public ActionResult SpotPromotion(string spchgpointid, string spmessage, string spstarttimetxt, string spendtimetxt, string spstartradio, string spchagingradio, string spendradio, string spvalueradio)
{
string filename = "5fa9d4e9-c2ee-4d34-8d83-a83a60ffa49d.xls";
string couponfrom = "zcc";
List<ChgPoint> ch = new List<ChgPoint>();
ch = SpotControllerCRUD.GetAllChagePoint();
return View("Index");
}

[CustomAuthorize(Roles = "admin")]
[HttpPost]
public ActionResult TragetPromotion(string tpchgpointid, string tpmessage, string tpstarttimetxt, string tpendtimetxt, string tpstartradio, string tpchagingradio, string tpendradio, string tpvalueradio)
{
string filename = "5fa9d4e9-c2ee-4d34-8d83-a83a60ffa49d.xls";
string couponfrom = "zcc";
List<ChgPoint> ch = new List<ChgPoint>();
ch = SpotControllerCRUD.GetAllChagePoint();
return View("Index");
}


but when press the submit button it didn't went to action method

Answer

Change the type = "submit" instead of "button".

If the type is button you will have to specify its action.

Comments