RedirectToUmbracoPage with custom model after form post?
How can I do a POST and RedirectToUmbracoPage() with a custom model. As far as I can tell, RedirectToUmbracoPage takes a nodeId, but I couldn't tell how to pass it my custom model. The model I have does inherit from RenderModel, but I am getting the error that my view expects my custom model, but it was passed a RenderModel.
Initially, I was just returning View("page"), but this causes another post on refresh, so what I would really like to do is redirect the user? Also, I am not sure if a POST is what I need because I am basically searching a database and returning results, I am actually not saving any data, but I am not sure how to do what I want with a GET?
In a nutshell, I want to search a database with some Form values, get the data into a model and redirect the user to another page with my model?
Below works without a custom model, now it is just a matter of getting that in there.
var nodeId = Convert.ToInt32(CurrentPage.GetProperty("NoProductsFoundUrl").DataValue);
return RedirectToUmbracoPage(nodeId);
It sounds to me that it's probably a GET that you need in this scenario. As you suggest, POSTs are intended (in general web development, not just in Umbraco) for when they trigger a change. For a search, it would sound like a GET would be more appropriate.
For your scenario I would take a look at route hijacking, and create a controller that responds to the particular doc type/template you are rendering. You can then pass additional parameters to that action method, populate a custom model as you need and return it to your view.
I accomplished it using a GET with @Html.BeginUmbracoForm, but it appends a upfrt to the url which is really long. I tried doing a normal @Html.BeginForm and form action..., but none of those change the view of the page. The only one that does is @Html.BeginUmbracoForm. Here is the code for the form:
public ActionResult Search(ProductFilterModel productFilterModel)
{
var db = new Database("productsDb");
var productSearchResultsModel = new ProductSearchResultsModel
{
ProductFilterModel = productFilterModel,
Products = db.Fetch<ProductViewModel>(@"SELECT DISTINCT(p.ProductID) As ProductId, Type, Name, PartNumber, ShortDescription
FROM Products p
INNER JOIN productlink pl on pl.ProductID = p.ProductID
INNER JOIN vehicles v on v.VehicleID = pl.VehicleID
WHERE Brand = 'SCT'
AND v.Make = @VehicleMakeId
AND v.Model = @VehicleModelId
AND v.Engine = @VehicleEngineId
AND v.Year = @VehicleYearId", new { productFilterModel.VehicleYearId, productFilterModel.VehicleMakeId, productFilterModel.VehicleModelId, productFilterModel.VehicleEngineId })
};
if(productSearchResultsModel.Products.Any())
return View("~/Views/ProductsFound.cshtml",productSearchResultsModel);
return View("~/Views/NoProductsFound.cshtml", productFilterModel);
}
I noticed if I do a normal form with a GET or do a @Html.BeginForm, it submits the form, but it calls calls javascript and doesn't render the new view with the product results. Any ideas on why this is happening?
Afraid not Saied, sorry. As far as I can see you should just have a form posting a GET request to the product search page. Using Html.BeginForm or just a <form /> element.
Something you could try is putting the model into a TempData field and then on your "no products found" page you check the TempData to see if it contains anything. If it does you use that instead of the model.
RedirectToUmbracoPage with custom model after form post?
How can I do a POST and
RedirectToUmbracoPage()
with a custom model. As far as I can tell,RedirectToUmbracoPage
takes a nodeId, but I couldn't tell how to pass it my custom model. The model I have does inherit fromRenderModel
, but I am getting the error that my view expects my custom model, but it was passed a RenderModel.Initially, I was just returning
View("page")
, but this causes another post on refresh, so what I would really like to do is redirect the user? Also, I am not sure if aPOST
is what I need because I am basically searching a database and returning results, I am actually not saving any data, but I am not sure how to do what I want with aGET
?In a nutshell, I want to search a database with some Form values, get the data into a model and redirect the user to another page with my model?
Below works without a custom model, now it is just a matter of getting that in there.
Thanks, Saied
It sounds to me that it's probably a GET that you need in this scenario. As you suggest, POSTs are intended (in general web development, not just in Umbraco) for when they trigger a change. For a search, it would sound like a GET would be more appropriate.
For your scenario I would take a look at route hijacking, and create a controller that responds to the particular doc type/template you are rendering. You can then pass additional parameters to that action method, populate a custom model as you need and return it to your view.
Hope that helps.
Andy
Hi Andy,
I accomplished it using a GET with
@Html.BeginUmbracoForm
, but it appends a upfrt to the url which is really long. I tried doing a normal@Html.BeginForm
andform action...
, but none of those change the view of the page. The only one that does is@Html.BeginUmbracoForm
. Here is the code for the form:The Search ActionResult method is here:
I noticed if I do a normal
form
with aGET
or do a@Html.BeginForm
, it submits the form, but it calls calls javascript and doesn't render the new view with the product results. Any ideas on why this is happening?Afraid not Saied, sorry. As far as I can see you should just have a form posting a GET request to the product search page. Using Html.BeginForm or just a
<form />
element.Hi Saied,
Something you could try is putting the model into a TempData field and then on your "no products found" page you check the TempData to see if it contains anything. If it does you use that instead of the model.
Nik,
I thought about doing this, but not wasn't sure if TempData was the right tool for the job?
Temp data is your best bet as is can persist against a change in request. However, you could also try ViewData first.
is working on a reply...