评级记录未在数据库中保存

时间:2016-05-27 19:17:39

标签: c# asp.net-mvc

控制器=>> view正在向SendRating发送数据,此方法负责将数据存储在数据库中,但数据未保存。 只有经过身份验证的用户才能评分,但每次我们面临评分失败时,我都无法理解问题到底是什么。

public ActionResult SendRating(string r, string s, string id, string url)
        {
            try {
                var context = new ApplicationDbContext();

            string currentUserId = User.Identity.GetUserId();

            int autoid = 0;
            Int16 thisVote = 0;
            Int16 O_ServID = 0;
            Int16.TryParse(s, out O_ServID);
            Int16.TryParse(r, out thisVote);
            int.TryParse(id, out autoid);

            if (!User.Identity.IsAuthenticated)
            {
                return Json("Not authenticated!");
            }



            switch (s)
            {
                case "5":
                        //var isIt = db.VoteModel.Where(v => v.O_ServID == O_ServID && v.UserId.Equals(User.Identity.Name, StringComparison.CurrentCultureIgnoreCase) && v.WorkerID == autoid).FirstOrDefault();


                        var service = context.OrderServices.Where(o => o.O_ServID == O_ServID).Single();
                        var worker = context.Workers.Where(w => w.WorkerID == autoid).Single();
                        var store = new UserStore<ApplicationUser>(new ApplicationDbContext());
                        var userManager = new UserManager<ApplicationUser>(store);
                        var user = userManager.FindByName(User.Identity.Name);
                        VoteModel vm = new VoteModel()
                    {
                        Active = true,
                        //id
                        UserId = User.Identity.GetUserId(),
                        Votes = thisVote,
                        WorkerID = autoid,
                        O_ServID = O_ServID,
                        OrderServices = service,Workers = worker,User = user
                    };

                    var save = context.VoteModel.Add(vm);

                       if(db.SaveChanges() > 0)
                        {
                            HttpCookie cookie = new HttpCookie(url, "true");
                            Response.Cookies.Add(cookie);

                            return Json("<br />You rated " + r + " star(s), thanks !", JsonRequestBehavior.AllowGet);

                        }


                    // keep the school voting flag to stop voting by this member
                    //HttpCookie cookie = new HttpCookie(url, "true");
                    //Response.Cookies.Add(cookie);
                    //}
                    break;
                default:
                    break;
            }
                return Json("Rating Failed!", JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                HttpCookie cookie = new HttpCookie(url, "true");
                Response.Cookies.Add(cookie);

                return Json(ex.Message, JsonRequestBehavior.AllowGet);

                throw;
            }
        }






        public ActionResult VoteNow(int id, int serv)
        {
            ViewData["id"] = id;
            ViewData["serv"] = serv;

            return View();
        }
        public ActionResult VoteShow()
        {
            return View();
        }

模型==&GT;&GT;

这是必须存储记录的表模式..

namespace WebApplication5.Models

{
    public class VoteModel
    {
        [Key]
        public int AutoId { get; set; }
        public int O_ServID { get; set; } 
        public OrderService OrderServices { get; set; }
        public int WorkerID { get; set; } 
        public Worker Workers { get; set; }
        public Int16 Votes { get; set; }
        public bool Active { get; set; }
        [Required]
        public string UserId { get; set; }
        public ApplicationUser User { get; set; }
    }

}

视图==&GT;&GT; 我的观点代码如下...... 使用json将数据发送到具有不同参数的控制器通过ViewData [] ..

@{
    ViewBag.Title = "VoteNow";
    Layout = "~/Views/Shared/_LayoutUser.cshtml";
}
<div id="page-wrapper">
    <div id="page-inner">
        <h2>VoteNow</h2>

        @model string
        @{
            var url = Request.Url.AbsolutePath;
        }
        @if (Request.Cookies[url] == null)
        {
            <div id="ratingDiv" class="smallText">
                Poor
                <img src="/img/whitestar.gif" alt="" class="ratingStar" data-value="1" /><img src="/img/whitestar.gif" alt="" class="ratingStar" data-value="2" /><img src="/img/whitestar.gif" alt="" class="ratingStar" data-value="3" /><img src="/img/whitestar.gif" alt="" class="ratingStar" data-value="4" /><img src="/img/whitestar.gif" alt="" class="ratingStar" data-value="5" /> Excellent
                <label id="lblResult"></label>
            </div>
            <style type="text/css">
                .ratingStar {
                    cursor: pointer;
                    height: 20px;
                    width: 20px;
                }
            </style>

        }
        else
        {
            <text><span style="color:green;"> Thanks for your vote !</span></text>
        }

        @section scripts{
            <script type="text/javascript">
                var clickedFlag = false;
                $(".ratingStar").mouseover(function () {
                    $(this).attr("src", "/img/yellowstar.gif").prevAll("img.ratingStar").attr("src", "/img/yellowstar.gif");
                });
                $(".ratingStar, #radingDiv").mouseout(function () {
                    $(this).attr("src", "/img/whitestar.gif");
                });
                $("#ratingDiv").mouseout(function () {
                    if (!clickedFlag) {
                        $(".ratingStar").attr("src", "/img/whitestar.gif");
                    }
                });
                $(".ratingStar").click(function () {
                    clickedFlag = true;
                    $(".ratingStar").unbind("mouseout mouseover click").css("cursor", "default");

                    var url = "/CDashboard/SendRating?r=" + $(this).attr("data-value") + "&s=@((int)ViewData["serv"])&id=@((int)ViewData["id"])&url=@url";
                    $.post(url, null, function (data) {
                        $("#lblResult").html(data);
                    });
                });
                $("#lblResult").ajaxStart(function () {
                    $("#lblResult").html("Processing ....");
                });
                $("#lblResult").ajaxError(function () {
                    $("#lblResult").html("<br />Error occured.");
                });
            </script>
        }
        </div>
</div>

0 个答案:

没有答案