Currently we're using view rendering for login form but as per new requirement we need to change to Controller rendering to perform some logic in backend during submission. This looks like as given below. We're using Glass mapper so given Sitecore field( Header,UsernameLabel,LoginLabel ) render the field value directly from Sitecore on form load.
login.cshtml
@model ILogin
<form id="loginForm" name="loginForm" action="/someurl" method="POST">
<h2>@Html.Glass().Editable(m => m.Header)</h2>
...
<label>@Html.Glass().Editable(m => m.UsernameLabel)</label>
...
<button type="submit">@Html.Glass().Editable(m => m.LoginLabel)</button>
</form>
We created new Controller Rendering and Login GET and POST Action method in Controller
GET
public ActionResult Login()
{
var model = _mvcContext.SitecoreService.GetItem<ILogin>(_mvcContext.DataSourceItem);
return View("~/public/Views/login.cshtml", model);
}
POST
[HttpPost]
public ActionResult Login(ILogin model)
{
}
During GET operation,login form is loading properly but getting below error during submission:
Cannot create an instance of an interface.
Description: An unhandled exception occurred.
Exception Details: System.MissingMethodException: Cannot create an instance of an interface.
[MissingMethodException: Cannot create an instance of an interface. Object type 'Models.ILogin'.]
System.Web.Mvc.DefaultModelBinder.CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
Then while searching online we found that create a new class and implement interface. That means in GET method we need to get data source item, fetch values from Sitecore, fill values in class properties and return new class model. In POST method, replace ILogin with new class. This seems alternate options but Is there any other way we can handle submission instead of adding new class?