TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
jimmit raval
NA
34
531
I want to pass the selected value from controller to view
Nov 5 2018 4:40 AM
Hi,
I am very new to asp.net MVC and have my first project in hand.
I am having 2 pages Home and Attendance.
In Home controller, i am getting 3 client names C1, C2, C3 from database in my dropdownlistfor.
View
@model WebApplication1.Models.ClientMdl
@{
ViewBag.Title =
"Index"
;
}
<h2>
ATOM
</h2>
@
using
(Html.BeginForm(
"Index"
,
"Home"
,FormMethod.Post,
new
{ id=
"Frm1"
}))
{
<table>
<tr></tr>
<tr>
<div>
Select Client name<br />
@Html.DropDownListFor(model => model.Client_Names,
new
SelectList(Model.Clients,
"Client_Names"
,
"Client_Names"
),
"Select Client"
,
new
{ onchange =
"document.getElementById('Frm1').submit();"
})
@* @Html.TextBoxFor(model => model.Client_Names)*@
@Html.ValidationMessageFor(model => model.Client_Names)
<input type=
"submit"
value=
"Submit"
/>
</div>
</tr>
</table>
}
I want user to select client name and click on submit. This saves the selected client name in a global variable which will be used across pages.
Home Controller
namespace
WebApplication1.Controllers
{
public
class
HomeController : Controller
{
MdlCnStringContextClass _mdlCntxtcls =
new
MdlCnStringContextClass();
GlobalVaCls GVaCls =
new
GlobalVaCls();
GlblAttendanceCls glblAttendanceCls =
new
GlblAttendanceCls();
ClientMdl clntMdl =
new
ClientMdl();
/* public ActionResult Index()
{
Client clntMdlObj = new Client();
clntMdlObj.Clients = GetAllClientNames();
return View(clntMdlObj);
}
private List<SelectListItem > GetAllClientNames()
{
List<SelectListItem> CNames = new List<SelectListItem>();
CNames = _mdlCntxtcls.clients.Select( c => new SelectListItem { Value=c.ClientID.ToString(),Text=c.Client_Names.ToString()}).ToList();
return (CNames);
}*/
[HttpGet]
public
ActionResult Index()
{
clntMdl.Clients = GetClientNames(clntMdl);
int
InOutRws = glblAttendanceCls.Attendance();
if
(GlobalVaCls.SelectedClient != null)
{
ViewBag.VBg = GlobalVaCls.SelectedClient;
ViewBag.InOtRwsCnt = InOutRws;
if
(InOutRws > 0)
{
var vClientNames = _mdlCntxtcls.Clients
.Select(c => c.Client_Names);
return
View(clntMdl);
}
else
{
return
View(clntMdl);
}
}
else
{
return
View(clntMdl);
}
}
[HttpPost ]
public
ActionResult Index(ClientMdl client)
{
int
InOutRws = glblAttendanceCls.Attendance();
ViewBag.InOtRwsCnt = InOutRws;
//Use this statement when getting the value of Dropdown text from View
string Client_Names = client.Client_Names.ToString();
//Gets the Client name from textbox in Index View
//string strClient = client.Client_Names;
//ViewBag.M = strClient;
GlobalVaCls.SelectedClient = client.Client_Names;
clntMdl.Clients = GetClientNames(clntMdl);
return
View(clntMdl);
}
}
When i am on Attendance page and clicking Home link to browse to Home page. I want the client name which was saved in global variable to be the text of the dropdownlist in Home page.
But i am facing an error as Object not set to an Instance in this statement
@Html.DropDownListFor(model => model.Client_Names,
new
SelectList(Model.Clients,
"Client_Names"
,
"Client_Names"
),
"Select Client"
,
new
{ onchange =
"document.getElementById('Frm1').submit();"
})
Here is where i need your help. Please revert if you need more clarifications.
Reply
Answers (
3
)
pass data between usercontrol
How to get data from a USB magnetic swipe card reader