I am new in .net, I am using MVC5, I have checked some .net example where people are doing Strongly type view to pass the data from controller to view. For that you should have a model where those parameter will set and then you can pass the data.
Suppose my model is User
namespace Mvc5Application.Models{
using System;
using System.Collections.Generic;
public partial class User
{
public System.Guid UserID { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public Nullable<System.Guid> OrgID { get; set; }
public virtual Organization Organization { get; set; }
}
}
Now my controller is Home and in the index page there have a login page
If you go with the traditional way then your controller will be look like
[AllowAnonymous]
public ActionResult Index()
{
ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
return View();
}
[HttpPost]
public ActionResult Index(User model)
{
...[code for verification ]...
return View(model);
}
And the corresponding view will be
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.username)
</td>
<td>
@Html.DisplayFor(modelItem => item.password)
</td>
</tr>
}
But if you are thinking to do some customization Like if you don't want to follow this traditional model and you want to post anything from view page, and get it in controller and after perform some operation if you have a set of data and you want to pass the data in to view and show it in different place in view then you have to use ViewBag Or ViewData
Then the controller will be
public ActionResult Index()
{
ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
return View();
}
[HttpPost]
public ActionResult Index(FormCollection form)
{
String Username = form["Username"].ToString();
String Password = form["Password"].ToString();
Dictionary<string, string> MyList = new Dictionary<String, String>();
MyList.Add("name",Username);
MyList.Add("pass",Password);
ViewData["user"] = MyList;
return View();
}
And the View will be
@{
ViewBag.Title = "Home Page";
var list = ViewData["user"] as Dictionary<String, String>;
}
<h2>@ViewBag.Message</h2>
<div>
@if (list != null)
{
@Html.Label("User:-") @list["name"]
<br />@Html.Label("password:-") @list["pass"]
}
</div>
<p>
Please ligin / Register from here</p>
@using (Html.BeginForm("Home"))
{
<table>
<tr>
<td>User Name</td>
<td>@Html.TextBox("Username", ViewBag.CurrentFilter as string)</td>
</tr>
<tr>
<td>Password</td>
<td>@Html.TextBox("Password", ViewBag.CurrentFilter as string)</td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Save" class="btn btn-default" /></td>
</tr>
</table>
}