在一个MVC 4视图asp.net中的多个viewmodel没有像预期的那样工作

本文关键字:viewmodel 工作 一个 MVC net asp 视图 | 更新日期: 2023-09-27 18:15:40

我有PromotionAdmin控制器和视图从用户获取数据。我想从视图中的多个表单中获取数据我有两个表单,像这样

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

我调用PromotionAdmin Controller我调用以下两个方法

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

但是当按下提交按钮时它没有转到动作方法

在一个MVC 4视图asp.net中的多个viewmodel没有像预期的那样工作

type = "submit"改为"button"。

如果typebutton,则必须指定其动作