-1

I need to post the selected item's id from dropdown to post method of same controller and the render the content in same view PLease help me fast :(

My View

    @Html.DropDownListFor(x => x.hotelmodel.SelectedHotelId, Model.hotelmodel.DrpHotelList)
<div>
<p>@Model.percentageoccupied</p>
 <p>@Model.Revenue</p>
 <p>@Model.UnSold</p>
<div>

MY HttpGetMethod

 [HttpGet]
    public ActionResult Counter()
    {
        var personid = ((PersonModel)Session["PersonSession"]).PersonId;
        var model = new CounterforModel();

        model.hotelmodel.DrpHotelList = iCommonservice.GetHotelListByPersonId(personid);

        return View(model);
    }

My HttpPostMethod

     [HttpPost]

        public ActionResult Counter(int id)
        {
            var result = iCommonservice.LoadCounter(id);
            model.percentageoccupied = Convert.ToInt32(result[0].percentageoccupied);
            model.Revenue = Convert.ToDecimal(result[0].Revenue);
            model.UnSold = Convert.ToInt32(result[0].UnSold);
            return View(model);
        }
Anuj Tamrakar
  • 81
  • 2
  • 9
  • i pass this SelectedHotelId to a service class which will fetch me list of percentage occupied, revenue and unsold values as you can see on the httpost method. – Anuj Tamrakar Mar 29 '15 at 11:33
  • the problem is the DropDownList don't post back the selected values. – Anuj Tamrakar Mar 29 '15 at 11:35
  • i havent implemented ajax call. i am new to ajax. i want to know the ajax syntax for my requirement. yes, the CounterforModel contain complex property named hotelmodel which contains a property int SelectedHotelid – Anuj Tamrakar Mar 29 '15 at 11:50
  • the parameter is just a prototype. i have updated my actionresult – Anuj Tamrakar Mar 29 '15 at 11:51

1 Answers1

0

In your POST method pass your View Model as parameter. Then you can use the posted hotelModel.SelectedHotelId for what you need, update the model values and pass your updated model to the View.

    public ActionResult Counter(CounterforModel model)
    {
        var result = iCommonservice.LoadCounter(model.hotelmodel.SelectedHotelId);
        model.percentageoccupied = Convert.ToInt32(result[0].percentageoccupied);
        model.Revenue = Convert.ToDecimal(result[0].Revenue);
        model.UnSold = Convert.ToInt32(result[0].UnSold);

        return View(model);
    }

If you want, you can use @Ajax.BeginForm() or jQuery .ajax() to make an ajax call to your action. You can look here.

Community
  • 1
  • 1
vortex
  • 1,048
  • 1
  • 10
  • 17