susan
susan

Reputation: 163

How to display Get and Post action result in same view in mvc5?

Hi i have one view called Sales Visit. In that view i have two textboxes FromDate and Todate. If i select the FromDate and Todate and click the ok button it pass the dates to post action of SalesVisit in Controller.

In the controller it calculated one values depends upon the dates criteria. Now i want to show that result which i calculated in the post action in controller in same view. Eg which is shown in the below image.

Example View

In the above image i have two textboxes if i select the from date and two date and click the ok button means it pass the dates to controller and in controller it calculate some value and again it have to pass the result to same view. Eg in the above image i draw two boxes with values (red colour boxes).How I bring that result.

My Model(Dashboard View Model)

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

My Controller Code

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

My view Code

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

I tried Partial view but it showing object reference not set to an instance of an object error. I tried my level best to explain my issue.Any one understand my issue and help me to resolve this issue.

Thanks.

Upvotes: 0

Views: 1965

Answers (1)

JanneP
JanneP

Reputation: 587

Try using Ajax.BeginForm instead of Html.BeginForm.

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

Add this to your view to direct the result to a partial view.

@Html.Partial("_sales")

Then return PartialViewResult from controller.

return PartialView("_sales", obj);

Create the partial view _sales and put a div inside it with the model data.

<div id="visitDiv">
  @model.sizingcount
</div>

Upvotes: 1

Related Questions