我有一个处理多个联系表单的控制器。我目前将模型传递给我的视图。现在构建它的方式是,当返回ContactSuccess.cshtml视图时,URL不会改变。我只需要能够更改URL,我的想法是在最后添加一个ID查询字符串。所以,而不是URL / contactus,我希望它是/ contactus?id = what。此项目未使用默认路由。
由于
这就是我现在所拥有的。这会导致页面出现以下错误:“视图”〜/ Views / Contact / ContactSuccess.cshtml?id = 3'或未找到其主页或视图引擎不支持搜索的位置。搜索了以下位置: 〜/查看/联系/ 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);
}
答案 0 :(得分:2)
呈现视图以响应HTTP请求。 HTTP请求以URL为目标。如果您想更改URL(您不能,因为您已经在响应请求),您必须对该请求的响应进行重定向。
为了让重定向后可以访问模型,请使用TempData。
因此,在您当前的操作方法中,创建模型并将其存储在TempData
中,然后重定向:
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 });
}
然后在您重定向到的操作的视图中:
public ActionResult Success(string id)
{
var model = TempData["RedirectModel"] as FooModel;
return View(model);
}
虽然我不知道你还需要id
。