Arun Raju
Arun Raju

Reputation: 149

how to send an obect using post to ActionResult

Here i have "bookAppointment" object i am redirecting to another actionresult with this object but the values of this object are displaying in the url like

http://localhost:54592/ServiceConsumer/AppointmentStatusPage?BookingID=401&RefBookingID=BED0414_401&SubLocationID=2&ProviderID=9&ProviderName=kalpana%20challa&ProviderEmail

but i dont want to display values of object i need to display until this http://localhost:54592/ServiceConsumer/AppointmentStatusPage

and this is my code:

 return RedirectToActionPermanent("AppointmentStatusPage", "ServiceConsumer", new RouteValueDictionary(bookAppointment));

and my actionresult:

public ActionResult AppointmentStatusPage(BookAppointment bookAppointment)
        {
            try
            {
                if (!string.IsNullOrEmpty(Session["UserID"].ToString()) && !string.IsNullOrEmpty(bookAppointment.TransactionStatus))
                {
                   return View(bookAppointment);
                }
                else
                    return RedirectToAction("guestsearch", "Home");
            }
            catch(Exception ex)
            {
                return null;
            }
        }

Upvotes: 0

Views: 65

Answers (2)

KD2ND
KD2ND

Reputation: 321

You can do something like the following :

in your first controller :

public ActionResult Index()
        {
           BookAppointment model = new BookAppointment() { TransactionStatus = "Passed" };
        return View(model);
        }

your first view :

@model BookAppointment


@{
    ViewBag.Title = "Index";
}


@using (Html.BeginForm("AppointmentStatusPage", "ServiceConsumer", FormMethod.Post))
{
    <input id="btnGo" type="submit" class="btn btn-sm btn-info" value="Go" />
    @Html.HiddenFor(model => model.TransactionStatus);
}

In your second controller :

[HttpPost]
  public ActionResult AppointmentStatusPage(BookAppointment bookAppointment)
{
    try
    {
        if (!string.IsNullOrEmpty(Session["UserID"].ToString()) && !string.IsNullOrEmpty(bookAppointment.TransactionStatus))
        {
            return View(bookAppointment);
        }
        else
            return RedirectToAction("guestsearch", "Home");
    }
    catch (Exception ex)
    {
        return null;
    }
}

Upvotes: 1

NBaua
NBaua

Reputation: 694

User decorate the Action result with HttpVerb (i.e POST) as under:

[HttpPost]
public ActionResult Index(Object obj)
{
  //Your code goes here...
}

For complete example visit this page:

https://www.aspsnippets.com/Articles/ASPNet-MVC-Form-Submit-Post-example.aspx

Hope this helps, mark this as answer if you get the solution right.

Regards,

N Baua

Upvotes: 0

Related Questions