In my last article, I talk about how to dynamically render WFFM
form in MVC by specifying hard coded form item Id. But that’s not a good thing.
Let’s take example of below code
@Html.Sitecore().Rendering("{F2CCA16D-7524-4E99-8EE0-78FF6394A3B3}", new
{
Datasource = "{C316B617-0306-454E-8729-E44F49B84CDD}",
UniqueId = "{C316B617-0306-454E-8729-E44F49B84CDD}"
});
How to pass form item id to this rendering
method?
Query String
One way to avoid hard coded GUID is to use query string. Pass
your form id as a query string as
Process this query string in your cshtml file as
@{
if (!string.IsNullOrEmpty(Request.QueryString["FormID"]))
{
string wffmFormGUID = Request.QueryString["FormID"];
//
Validate GUID before passing to Rendering method
@Html.Sitecore().Rendering("{F2CCA16D-7524-4E99-8EE0-78FF6394A3B3}", new { Datasource = wffmFormGUID, UniqueId =
wffmFormGUID });
}
}
The problem with query sting is that users can view, alter them
and that can affect the rendering process. Also from SEO prospective query
string is not a good option.
Model
Better option is pass the WFFM form id with model in MVC Action
Method as
public ActionResult RenderWFFMForm()
{
Messanger obj = new Messanger();
obj.WFFMFormId = "{354813EE-441A-472B-84BD-EB430B26B734}";
return View("RenderWFFMForm", obj);
}
Use below code in view
<div id="wffmFormDiv">
@{
if (string.IsNullOrEmpty(Model.WFFMFormId))
{
string wffmFormID = Model.WFFMFormId;
if (!string.IsNullOrEmpty(wffmFormID))
{
<div>
@Html.Sitecore().Rendering("{F2CCA16D-7524-4E99-8EE0-78FF6394A3B3}", new
{
Datasource = wffmFormID,
UniqueId
= wffmFormID
})
</div>
}
}
}
</div>
You can also
pass form id using TempData, ViewData or ViewBag as well.
I hope you like this Sitecore tip. Stay tuned for more Sitecore related
articles.
Till that happy sitecoring :)