2个按钮,用于根据btn mvc5设置模型属性

时间:2019-04-30 01:56:29

标签: c# razor asp.net-mvc-5 asp.net-identity

我有2个按钮,但无法将模型带到页面上,然后单击一个按钮来确定如何设置模型。我在想,如果我可以在页面上找到我的模型,我将可能会找出这些按钮。我尝试像往常一样将模型带入新的{model = Model},但是我的模型仍然为空……这是我的代码

// GET: TimeSheet/AdminApproval{masterid}
        public ActionResult AdminApproval(int masterId)
        {
            var masterModel = context.TimeSheetMaster.Where(w =>
                              w.TimeSheetMasterId.Equals(masterId)).FirstOrDefault();
            var detailM = context.TimeSheetDetails.Where(t => t.TimeSheetMasterId.Equals(
                              masterModel.TimeSheetMasterId)).FirstOrDefault();
            var project = context.Projects.Where(p => p.ProjectId == 
                              detailM.ProjectId).FirstOrDefault();

            var details = (from master in context.TimeSheetMaster
                           join detail in context.TimeSheetDetails
                           on master.TimeSheetMasterId equals detail.TimeSheetMasterId
                           where master.TimeSheetMasterId == masterId
                           select new TimeSheetDetailsModel()
                           {
                               Sunday = detail.Sunday,
                               Monday = detail.Monday,
                               Tuesday = detail.Tuesday,
                               Wednesday = detail.Wednesday,
                               Thursday = detail.Thursday,
                               Friday = detail.Friday,
                               Saturday = detail.Saturday,
                               Hours = detail.Hours,
                               Comment = master.Comment,
                               ProjectName = project.ProjectName
                           }).FirstOrDefault();
            return View(details);
        }
        //POST: TimeSheet/AdminApproval{model}{approve}{reject}
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> AdminApproval(TimeSheetDetailsModel model, 
               string btnApprove, string btnReject)
        {
            var master = await context.TimeSheetMaster.Where(m => m.TimeSheetMasterId == 
                         model.TimeSheetMasterId).FirstOrDefaultAsync();

            if(btnApprove != null)
            {

                return RedirectToAction("TimeSheetList");
            }
            else if(btnReject != null)
            {

                return RedirectToAction("Index", "Home");
            }
            else
            {

            }
            return View();
        }                    <div class="panel-footer">
                        @using (Html.BeginForm("AdminApproval", "TimeSheet",
                            new { model = Model }, FormMethod.Post))
                        {
                            @Html.AntiForgeryToken()
                            <input type="submit" class="btn btn-success" value="Approve" name="btnApprove" />

                            <input type="submit" class="btn btn-danger" value="Reject" name="btnReject" />

                        }

我尝试了很多方法,但是没有运气...甚至尝试了评论方法。 在httppost中获取mmy模型

    [HttpPost]
        public ActionResult AdminApproval(TimeSheetDetailsModel model)
        {
            var master =  context.TimeSheetMaster.Where(m => m.TimeSheetMasterId ==
                         model.TimeSheetMasterId).FirstOrDefaultAsync();
            return RedirectToAction("TimeSheetList");
        }


        public ActionResult ApproveTimeSheet(TimeSheetDetailsModel model)
            //([Bind(Include ="TimeSheetMasterId,UserId")]TimeSheetDetailsModel model)
        {
            return RedirectToAction("TimeSheetList");
        }

        [HttpPost]
        public ActionResult RejectTimeSheet([Bind(Include = "TimeSheetMasterId,UserId")]TimeSheetDetailsModel model)
        {
            return RedirectToAction("Index", "Home");
        }
        //POST: TimeSheet/AdminApproval{model}{approve}{reject}//[ValidateAntiForgeryToken]
        /*[HttpPost]        
        public async Task<ActionResult> AdminApproval(int id, TimeSheetDetailsModel model )
        // ,string btnApprove, string btnReject)
        //{
            var master = await context.TimeSheetMaster.Where(m => m.TimeSheetMasterId ==
                         model.TimeSheetMasterId).FirstOrDefaultAsync();

            //if (btnApprove != null)
            //{

                return RedirectToAction("TimeSheetList");
            //}
 @using (Html.BeginForm())
                        {
                            <input type="submit" value="Approve" name="TimeStat" />
                        }
                        @*using (Html.BeginForm())
                        {*@
                            @*@Html.AntiForgeryToken()*@
                            <input type="submit" class="btn btn-success" value="Approve" name="btnTime" 
                                   formaction="@Url.Action("RejectTimeSheet")" formmethod="post" />

                            <input type="submit" class="btn btn-danger" value="Reject" name="btnTime"
                                   formaction="@Url.Action("ApproveTimeSheet")" formmethod="post" />

                        @* } *@

                        @*@Html.ActionLink("Approve2", "AdminApproval", new { model = Model }, new { @class = "btn btn-success" })*@

                        @*@Html.ActionLink("Reject", "", null, new { @class = "btn btn-danger" })*@

                        @Html.ActionLink("App", "ApproveTimeSheet", new { model = Model })

                        @Html.ActionLink("Rej", "RejectTimeSheet", new { model = Model.TimeSheetMasterId })

                        <a href="@Url.Action("ApproveTimeSheet", "TimeSheet", new { model = Model })">hrefApp</a>

0 个答案:

没有答案