如何在mvc5中的同一视图中显示获取和发布操作结果?

时间:2016-10-22 08:07:37

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

您好我有一个名为Sales Visit的视图。在该视图中,我有两个文本框 FromDate Todate 。如果我选择FromDate和Todate并单击确定按钮,它会传递日期以在Controller中发布SalesVisit的操作。

在控制器中,它计算出一个值取决于日期标准。现在我想在同一视图中显示我在控制器中的post动作中计算出的结果。例如,如下图所示。

Example View

在上面的图片中我有两个文本框,如果我选择从日期和两个日期,然后单击确定按钮表示它将日期传递给控制器​​,在控制器中它计算一些值,并再次将结果传递给相同的视图。 例如,在上面的图像中,我绘制了两个带有值的框(红色框)。我如何得出结果。

我的模型(信息中心视图模型)

public class DashboardViewmodel
  {
   public List<CustomerTypeCountModel> CustomerTypesCountModels { get; set;}
    public List<View_VisitorsForm> Visits { get; set; }
    public CustomerTypeViewModel CustomerTypeViewModels { get; set; }
    public int sizingcount { get; set; }
    public int Processingcount { get; set; }
}

我的控制器代码

     public ActionResult SalesVisit()
      {
        return View();
      }
   [HttpPost]
   public ActionResult GetDatesFromSalesVisit(DashboardViewmodel dvm)
    {

        var fromdate = Convert.ToDateTime(dvm.CustomerTypeViewModels.FromDate);
        var todate = Convert.ToDateTime(dvm.CustomerTypeViewModels.ToDate);

        var userID = System.Web.HttpContext.Current.Session["UserID"].ToString();
        var objEmpDepUTID = db.UserRightsSettings.Where(u => u.UserID.ToString() == userID).Select(e => new
        {
            objemployeeID = e.EmployeeID,
            objdepartmentID = e.DepartmentID,
            objusertypeID = e.UserTypeID
        }).FirstOrDefault();

        var EmployeeID = objEmpDepUTID.objemployeeID;
        var DepartmentID = objEmpDepUTID.objdepartmentID;
        var UserTypeID = objEmpDepUTID.objusertypeID;
        var processingdeptid = (from d in db.Departments where d.DisplayName == "Processing" select d.DepartmentID).FirstOrDefault();
        var sizingdeptid = (from d in db.Departments where d.DisplayName == "Sizing" select d.DepartmentID).FirstOrDefault();

        List<View_VisitorsForm> objsizingcount = new List<View_VisitorsForm>();
        List<View_VisitorsForm> objprocessingcount = new List<View_VisitorsForm>();

        if (DepartmentID == new Guid("47D2C992-1CB6-44AA-91CA-6AA3C338447E") &&
            (UserTypeID == new Guid("106D02CC-7DC2-42BF-AC6F-D683ADDC1824") ||
            (UserTypeID == new Guid("B3728982-0016-4562-BF73-E9B8B99BD501"))))
        {
           var sizingview_visitorsforms = db.View_VisitorsForm.Where(x => x.EmpDepID == processingdeptid && x.VisitingDate >= fromdate 
                && x.VisitingDate <= todate).ToList();
           var processingview_visitorsform = db.View_VisitorsForm.Where(x => x.EmpDepID == sizingdeptid && x.VisitingDate >= fromdate 
                && x.VisitingDate <= todate).ToList();

            objsizingcount = sizingview_visitorsforms;
            objprocessingcount = processingview_visitorsform;
         }
        DashboardViewmodel obj = new DashboardViewmodel();
        obj.sizingcount = objsizingcount.Count();
        obj.Processingcount = objprocessingcount.Count();
        return View("SalesVisit", obj);
    }

我的观点代码

  @model  CostToWafe.Areas.Sales.Models.DashboardViewmodel
  @using  CostToWafe.Areas.Sales.Models
  @{
    ViewBag.Title = "SalesVisit";
    Layout = "~/Views/Shared/_Layout.cshtml";
   }

   @using (Html.BeginForm("GetDatesFromSalesVisit","AdminDashboard"))
  {
   @Html.AntiForgeryToken()
   @Html.ValidationSummary(true)
   <body>
   <div class="col-sm-3">
   <div class="form-group">
    @Html.Label("From Date")
    @Html.TextBoxFor(m => m.CustomerTypeViewModels.FromDate, new { @class = "form-control", id = "FromDate" })
    </div>
    </div>

   <div class="col-sm-3">
   <div class="form-group">
   @Html.Label("To Date")
   @Html.TextBoxFor(m => m.CustomerTypeViewModels.ToDate, new { @class = "form-control", id = "ToDate" })
   </div>
   </div>

我尝试了部分视图,但它显示对象引用没有设置为对象的实例错误。我尽力解释我的问题。任何人都能理解我的问题并帮助我解决这个问题。

感谢。

1 个答案:

答案 0 :(得分:1)

尝试使用Ajax.BeginForm而不是Html.BeginForm。

select concat(round(1024*1048576/1073741824,2),' GB')

将其添加到视图中以将结果定向到部分视图。

@using (Ajax.BeginForm("GetDatesFromSalesVisit", "AdminDashboard", new AjaxOptions {
    UpdateTargetId = "visitDiv" 
}

然后从控制器返回PartialViewResult。

@Html.Partial("_sales")

创建局部视图_sales并在其中放入包含模型数据的div。

return PartialView("_sales", obj);