Reputation: 2297
I have create one simple page in MVC, which is given in startup project of MVC by .net framework with slight modification in that.
I have created two models
Create two controllers.
Then i have use both of them to display in my home page (like facebook just an example)
Model code: Login Model:
public class Login
{
private string email;
[Required]
public string Email
{
get { return email; }
set { email = value; }
}
private string password;
[Required]
public string Password
{
get { return password; }
set { password = value; }
}
}
Register Model
public class Register
{
private string email;
[Required]
public string Email
{
get { return email; }
set { email = value; }
}
private string password;
[Required]
public string Password
{
get { return password; }
set { password = value; }
}
private string name;
[Required]
public string Name
{
get { return name; }
set { name = value; }
}
}
View of Login and Register both is created using "Create" Option. Both contains
<input type="submit" value="Create" />
Now, both views are on my other page, home page.
@Html.Action("Index", "Login")
@Html.Action("Index", "Register")
Both display okay, but when i click on any of the "create" button of any of the view, it also gets fire the event in controller for the other one.
My controller code....Logincontroller.
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(Login lgobj)
{
System.Threading.Thread.Sleep(2000);
string email = lgobj.Email;
string password = lgobj.Password;
return View();
}
RegisterController:
[HttpGet]
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(Register model)
{
return View();
}
Can anyone please specify the reason, or what is missing in my code?
If not clear, please let me know, i will be describe more.
Upvotes: 0
Views: 915
Reputation: 2542
In this scenario, ( as commented in my first answer)
We need to use a Dynamic view page. (More Information) Follow following steps:
Create DynamicViewPage type:
public class DynamicViewPage : ViewPage
{
public new dynamic Model { get; private set; }
protected override void SetViewData(ViewDataDictionary viewData)
{
base.SetViewData(viewData);
Model = ViewData.Model;
}
}
Your Controller will look like
public ActionResult Account(string returnUrl) { LoginModel loginmodel = null;//Initialize Model; RegistrationModel registrationModel = null ;//Initialize Model; . . . . .
return View("Account", new
{
Login = loginmodel,
Register = registrationModel
});
your View should Inherit from
Inherits="DynamicViewPage"
Now @Model.Login will give you Loginmodel
@Model.Register will give you RegisterModel
It should work as you expected.........
Upvotes: 0
Reputation: 2542
Little change to what mentioned by @Shark,
We need to mention the action and controller to which indivisual form will post data, If we will not specify both the form will post to the page url.
@using (Html.BeginForm("Index","Login"))
{
@Html.Action("Index", "Login")
}
@using (Html.BeginForm("Index","Register"))
{
@Html.Action("Index", "Register")
}
Upvotes: 0
Reputation: 6011
Ok so you have a page (a View) that displays two ActionLinks. Once clicked, these two ActionLinks brings you to the LoginController or the RegisterController (depending on the link you’ve clicked). In both cases, you end up inside the Index() ActionResult of the appropriate Controller…so far so good!
Now…once inside the Index() you simply call return View() but I don’t see any code showing that these Views are strongly typed! Try the following:
LoginController:
public ActionResult Index()
{
var model = new Login();
return View(model);
}
RegisterController:
public ActionResult Index()
{
var model = new Register();
return View(model);
}
Assuming you’ve created two strongly typed views (Index.cshtml inside the Login folder and Index.cshtml inside the Register folder) make sure each view has an appropriate form and a submit button.
Upvotes: 0
Reputation:
Try changing your Home View to this:
@using (Html.BeginForm())
{
@Html.Action("Index", "Login")
}
@using (Html.BeginForm())
{
@Html.Action("Index", "Register")
}
What I think is happening is that your submit is calling a POST
to the wrong controller, as there is no distinct difference of forms.
Upvotes: 1