coggicc
coggicc

Reputation: 255

Return View with model & query string

I have a controller that handles several contact forms. I currently pass a model to my view. The way this is built now the URL does not change when the ContactSuccess.cshtml view is returned. I simply need to be able to change the URL, and my thinking was to add an ID querystring to the end. So instead of the URL being /contactus I want it to be /contactus?id=whatever. This project is not using the default routing.

Thanks

Here is what I have right now. This blows up the page with the following error "The view '~/Views/Contact/ContactSuccess.cshtml?id=3' or its master was not found or no view engine supports the searched locations. The following locations were searched: ~/Views/Contact/ContactSuccess.cshtml?id=3".

[HttpPost]
public ActionResult ContactUs(ContactFormViewModel data, string[] sections)
{
    var id = "3";

    var page = _logic.GetPage(SectionArea, PhiferBusinessLogic.PageTypes.ContactUs);
    var newModel = new ContactPageViewModel(page) { FormViewModel = new ContactFormViewModel { TollFreePhone = _logic.GetAreaTollFreeNumber(SectionArea) }};
    var content = _logic.GetPageContent(page.CMSPageID);
    newModel.FormViewModel = data;
    newModel.FormViewModel.ShouldShowContactIntroText = (SectionArea != PhiferBusinessLogic.SiteAreas.DrawnWire && SectionArea != PhiferBusinessLogic.SiteAreas.EngineeredProducts);
    newModel.FormViewModel.AreaString = newModel.AreaString;

    PhiferContactLogic pcl = new PhiferContactLogic();

    if (content != null)
    {
        newModel.ContentHTML = content.ContentHTML;
    }
    newModel.FormViewModel.Contact = data.Contact;
    if (page.CMSAreaID != 2 && String.IsNullOrWhiteSpace(data.Contact.Company))
    {
        ModelState.AddModelError("Contact.Company", "Company is required.");
    }

    ModelState.Remove("Sections");
    if (ModelState.IsValid)
    {
        var contact = new Contact();
        contact.Name = data.Contact.Name;
        contact.Email = data.Contact.Email;
        contact.Phone = data.Contact.Phone;
        contact.City = data.Contact.City;
        contact.State = data.Contact.State;
        contact.Zip = data.Contact.Zip;
        contact.Company = data.Contact.Company;
        contact.Comments = data.Contact.Comments;
        contact.Address = data.Contact.Address;
        contact.Website = data.Contact.Website;
        contact.PhiferAccountRep = data.Contact.PhiferAccountRepresentative;
        contact.Country = data.Contact.Country;
        contact.IP = data.Contact.IP;
        contact.EmailTemplate = data.Contact.EmailTemplate;

        var sectionSlugs = new List<string>();
        sectionSlugs.Add(GetAreaContactSectionSlug(SectionArea));
        if (sections != null)
        {
            sectionSlugs.AddRange(sections);
        }

        pcl.CreateContact(contact, sectionSlugs.ToArray());
        //current email method call
        pcl.EmailContactFormContacts(contact, sectionSlugs.ToArray());
        //send email to visitor
        Email.Email.GenerateTemplate(contact.Name, contact.Email, contact.EmailTemplate);
        return View("~/Views/Contact/ContactSuccess.cshtml", newModel);
    }

    newModel.BreadCrumbHTML = "<a href='/" + SectionSlug + "'>" + SectionName + "</a> > <span>Contact Us</span>";
    //This is the current return View
    //return View("~/Views/Contact/Contact.cshtml", newModel);
    return View ("~/Views/Contact/Contact.cshtml?id=" + id, newModel);            
}

Upvotes: 2

Views: 10891

Answers (1)

CodeCaster
CodeCaster

Reputation: 151594

A view is rendered in response to an HTTP request. An HTTP request is targeted to an URL. If you want to change the URL (which you can't, as you're already responding to the request), you'll have to make the response to that request a redirect.

In order to let a model be accessible after a redirect, use TempData.

So in your current action method, create the model and store it in TempData and then redirect :

public ActionResult View1()
{
    // The ActionMethod this question is about.
    // Do some magic.

    string id = 3;

    var model = new FooModel();

    TempData["RedirectModel"] = model;

    return RedirectToAction("Contact", "Success", new { id = id });
}

Then in the view for the action you redirect to:

public ActionResult Success(string id)
{
    var model = TempData["RedirectModel"] as FooModel;

    return View(model);
}

I don't know what you then still need the id for though.

Upvotes: 4

Related Questions