2

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);            
}
5
  • are you using default routing? that takes id as an optional param routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); Commented Aug 4, 2015 at 15:04
  • Is the id being passed into the action? If not, why does it need to be added to the querystring after the action has already been called? Why not store it in a model property or in the ViewBag? Commented Aug 4, 2015 at 15:05
  • Possible duplicate of MVC how to return a view with a parameter Commented Aug 4, 2015 at 15:10
  • I updated my question to better clarify what I need. Commented Aug 4, 2015 at 15:34
  • @CodeCaster I did not downvote this. I appreciate your input. Commented Aug 4, 2015 at 16:30

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.