.net mvc 视图提交list表单 后端action接收list表单

直接上代码

重要代码为红色部分;

视图代码如下:

@using DFCrm.UI.Web.Model
@using DFCrm.Domain.Entities.Bases
@using DFCrm.Domain.Entities.KpiBases
@using DFCrm.Infrastructure.Tokens
@using DFCrm.Domain.Entities.DepartmentPositions

@model List<KpiBaseTeam>
@{
    ViewBag.Title = "设置班级业绩指标";
    ViewBag.Column = "基地";
    Layout = "~/Views/Shared/_HomeLayout.cshtml";

    EmployeeToken token = ViewBag.Token;
    List<PermissionOperationEnum> permissions = ViewBag.Permissions;

    List<Base> bases = ViewBag.Bases;
    //List<KpiBaseTeam> kpibaseTeams = ViewBag.KpiBaseTeams;

    //#region 修改处理
    //if (Model.Count>0&& Model[0].Team!=null&& Model[0].Team.BaseId > 0)
    //{
    //    //基地
    //    if (!bases.Contains(Model[0].Team.Base))
    //    {
    //        bases.Add(Model[0].Team.Base);
    //    }
    //}
    //#endregion

    string fromUrl = !string.IsNullOrEmpty(Request.QueryString["fromUrl"]) ? Request.QueryString["fromUrl"] : Request.UrlReferrer != null ? Request.UrlReferrer.ToString() : null;
    string thisUrl = Request.Url != null ? Request.Url.ToString() : null;
}
<script type="text/javascript">
    $(document).ready(function (e) {
        selectedMenuWithPath(e, "/KpiBaseTeam");

        $("#btnClickCheck").click(function () {
            var baseId = $("#baseId").val();
            if (baseId == null || baseId == "" || baseId == 0) {
                alert("请选择基地")
                return false;
            }
        });
    });
</script>

<script>
    function setDate(num) {
        //当前日期
        var d = new Date();
        //alert(d.toLocaleDateString())
        //月初
        var f = d.setDate(1);
        f = new Date(f);
        f = f.setMonth(num);
        f = new Date(f);
        //alert(f.toLocaleDateString());
        $("#startDate").val(f.format("yyyy-MM-dd"));

        //月末
        var e = d.setDate(1);
        e = new Date(e);
        e = e.setMonth(num);
        e = new Date(e);
        e = e.setMonth(e.getMonth() + 1) - 24 * 60 * 60 * 1000;
        e = new Date(e);
        //alert(e.toLocaleDateString());
        $("#endDate").val(e.format("yyyy-MM-dd"));
    }

    Date.prototype.format = function (fmt) {
        var o = {
            "M+": this.getMonth() + 1,               //月份
            "d+": this.getDate(),                    //
            "h+": this.getHours(),                   //小时
            "m+": this.getMinutes(),                 //
            "s+": this.getSeconds(),                 //
            "q+": Math.floor((this.getMonth() + 3) / 3), //季度
            "S": this.getMilliseconds()             //毫秒
        };
        if (/(y+)/.test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        }
        for (var k in o) {
            if (new RegExp("(" + k + ")").test(fmt)) {
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
            }
        }
        return fmt;
    }


    function OnLoadTeam() {
        var baseId = $("#baseId").val();
        $.ajax({
            type: "GET",
            url: "/KpiBaseTeam/GetTeams?baseId=" + baseId,
            dataType: "json",
            success: function (data) {
                console.log(data);
                $("#KpiContents").empty();
                for (var i = 0; i < data.length; i++) {
                    $("#KpiContents").append("<div class=\"form-group\">  <label for=\"KpiTeam\">" + data[i].Name + ":</label><br /> <label>班级业绩指标:</label><div class=\"input-group\"><input name=\"Model[" + i + "].Team.Name\" value=\"" + data[i].Name + "\" hidden=\"hidden\" /><input name=\"Model[" + i + "].TeamId\" value=\"" + data[i].Id + "\" hidden=\"hidden\" />  <input name=\"Model[" + i + "].KpiTeam\" type=\"text\" class=\"form-control\" value=\"0.00\" /> <span class=\"input-group-addon\">元</span></div> <br /> <label> 自主开发业绩指标:</label ><div class=\"input-group\"><input name=\"Model[" + i + "].DIYKpi\" type=\"text\" class=\"form-control\" value=\"0.00\" /><span class=\"input-group-addon\">元</span></div> </div>");
                }
            }
        });
    }
</script>

<div class="row">
    <div class="col-md-12">
        @if (!string.IsNullOrEmpty(fromUrl) && fromUrl != thisUrl)
        {
            <a href="@fromUrl" class="btn btn-info"><i class="fa fa-chevron-left"></i> 返回</a>
        }
        else
        {
            <a href="@Url.Action("Index", "KpiBaseTeam")" class="btn btn-info"><i class="fa fa-chevron-left"></i> 返回</a>
        }
    </div>
    <div class="col-md-12">
        @using (Html.BeginForm("Detail", "KpiBaseTeam", new { fromUrl = fromUrl }, FormMethod.Post, new { enctype = "multipart/form-data" }))
        {
            <div class="panel panel-body bk-bg-white">
                <div class="form-group">
                    <h3>设置基地班级业绩指标</h3>
                </div>
                <div class="form-group">
                    <label for="baseId">基地:</label>
                    <select id="baseId" name="Model[0].Team.baseId" class="form-control" onchange="OnLoadTeam()">
                        <option value="0">请选择基地</option>
                        @foreach (var base1 in bases)
                        {
                            <option value="@base1.Id" @(base1.Id == (Model.Count > 0 ? Model[0].Team.BaseId : 0) ? "selected" : "")>@base1.Name</option>
                        }
                    </select>
                </div>
                <div class="form-group">
                    <label>日期区间:</label>
                    <div class="input-daterange input-group" data-plugin-datepicker>
                        <span class="input-group-addon"></span>
                        <input type="text" autocomplete="off" class="form-control" id="startDate" name="Model[0].startDate" value="@(Model.Count>0?Model[0].StartDate.ToString("yyyy-MM-dd"):DateTime.Now.ToString("yyyy-MM-dd"))" />
                        <span class="input-group-addon"></span>
                        <input type="text" autocomplete="off" class="form-control" id="endDate" name="Model[0].endDate" value="@(Model.Count>0?Model[0].EndDate.ToString("yyyy-MM-dd"):DateTime.Now.ToString("yyyy-MM-dd"))" />
                    </div>
                </div>
                <div class="form-group">
                    <botton onclick="setDate(0);" class="btn btn-default">1月</botton>
                    <botton onclick="setDate(1);" class="btn btn-default">2月</botton>
                    <botton onclick="setDate(2);" class="btn btn-default">3月</botton>
                    <botton onclick="setDate(3);" class="btn btn-default">4月</botton>
                    <botton onclick="setDate(4);" class="btn btn-default">5月</botton>
                    <botton onclick="setDate(5);" class="btn btn-default">6月</botton>
                    <botton onclick="setDate(6);" class="btn btn-default">7月</botton>
                    <botton onclick="setDate(7);" class="btn btn-default">8月</botton>
                    <botton onclick="setDate(8);" class="btn btn-default">9月</botton>
                    <botton onclick="setDate(9);" class="btn btn-default">10月</botton>
                    <botton onclick="setDate(10);" class="btn btn-default">11月</botton>
                    <botton onclick="setDate(11);" class="btn btn-default">12月</botton>
                </div>

                <div id="KpiContents">
                    @if (Model.Count > 0)
                    {
                        <div class="form-group">
                            <label for="KpiTeam">@Model[0].Team.Name:</label><br />
                            <label>班级业绩指标:</label>
                            <div class="input-group">
                                <input name="Model[0].Team.Name" value="@Model[0].Team.Name" hidden="hidden" />
                                <input name="Model[0].TeamId" value="@Model[0].TeamId" hidden="hidden" />
                                <input name="Model[0].Id" value="@Model[0].Id" hidden="hidden" />
                                <input name="Model[0].KpiTeam" type="text" class="form-control" value="@Model[0].KpiTeam" />
                                <span class="input-group-addon">元</span>
                            </div>
                            <br />
                            <label>自主开发业绩指标:</label>
                            <div class="input-group">
                                <input name="Model[0].DIYKpi" type="text" class="form-control" value="@Model[0].DIYKpi" />
                                <span class="input-group-addon">元</span>
                            </div>
                        </div>
                    }
                </div>



                <div class="form-group">
                    <input type="hidden" value="@Model.Count>0?Model[0].Id:0" name="Id" />
                    <button id="btnClickCheck" type="submit" name="actionType" value="save" onclick="btnDisabled(this);" class="btn btn-primary">保存</button>
                </div>
                @if (Model.Count > 0 && !string.IsNullOrEmpty(Model[0].OperationRecord))
                {
                    <div class="form-group">
                        <div>操作记录</div>
                        <div>@Html.Raw(Server.HtmlDecode(Model[0].OperationRecord.Replace("\r\n", "<br>")))</div>
                    </div>
                }
            </div>
        }
    </div>
</div>

后端action代码如下:

[HttpPost]
        public ActionResult Detail(List<KpiBaseTeam> model)
        {
            if (model.Count==1 && model[0].Id != 0)
            {
                this.KpiBaseTeamApplication.Update(model[0].TeamId, model[0].StartDate, model[0].EndDate, model[0].KpiTeam,model[0].DIYKpi, model[0].OperationRecord, model[0].Id);
            }
            else
            {
                this.KpiBaseTeamApplication.AddAllTeam(model);
            }
            if (!string.IsNullOrEmpty(Request.QueryString["fromUrl"]))
            {
                return Redirect(Request.QueryString["fromUrl"]);
            }
            else
            {
                return RedirectToAction("Index", "KpiBaseTeam");
            }
        }

需要用到的类:

public class KpiBaseTeam : AggregateRoot
    {
        /// <summary>
        /// 班级Id
        /// </summary>
        public int TeamId { get; set; }

        /// <summary>
        /// 班级
        /// </summary>
        [ForeignKey("TeamId")]
        public virtual Team Team { get; set; }

        /// <summary>
        /// 开始日期
        /// </summary>
        public DateTime StartDate { get; set; }

        /// <summary>
        /// 结束日期
        /// </summary>
        public DateTime EndDate { get; set; }

        /// <summary>
        /// 班级业绩
        /// </summary>
        [DecimalPrecision(18, 2)]
        public decimal KpiTeam { get; set; }
        /// <summary>
        /// 自主开发业绩
        /// </summary>
        [DecimalPrecision(18, 2)]
        public decimal DIYKpi { get; set; }

        /// <summary>
        /// 操作记录
        /// </summary>
        public string OperationRecord { get; set; }

        /// <summary>
        /// 添加日期
        /// </summary>
        public DateTime CreateDateTime { get; set; }
    }

public class Team : AggregateRoot
    {
        /// <summary>
        /// 基地Id
        /// </summary>
        public int BaseId { get; set; }

        /// <summary>
        /// 基地
        /// </summary>
        [ForeignKey("BaseId"), JsonIgnore]
        public virtual Base Base { get; set; }

        /// <summary>
        /// 班级名称
        /// </summary>
        [MaxLength(100, ErrorMessage = "班级名称最大为{1}个字符"), Required(ErrorMessage = "班级名称不能为空")]
        public string Name { get; set; }

        /// <summary>
        /// 班级员工
        /// </summary>
        [JsonIgnore]
        public virtual List<TeamEmployee> TeamEmployees { get; set; }

        /// <summary>
        /// 是否显示
        /// </summary>
        public bool Show { get; set; }
    }

 

.net mvc 视图提交list表单 后端action接收list表单

上一篇:C语言文件操作函数大全


下一篇:Python标准库——走马观花