Can you post the complete javascript block from the view? That error normally indicates that an operation is trying to be performed on an undefined object.
public ActionResult NewAccount(int? supervisorId)
{
Account account = new Account();
ViewData.Model = account;
ViewData["Supervisor"] = from ac in _accountService.GetAll().Where(ac => ac.IsManager == true).OrderBy(ac => ac.FirstName).ToList()
select new { AccountId = ac.AccountId, Name = ac.FirstName + " " + ac.LastName };
if (supervisorId != null)
{
}
return View("HRAddAccount");
}
This view has some other textboxes that get their values set after the user selects a supervisor, it will go back to the controller and makes a call to the database
then get the values for the remaining textboxes wich are read only then redisplay the NewAccount view.
You are right, I got it working now after I removed another javascript that was refering to in the Master Page.
However, I got another problem. When I render the same view after the user selects an item from the Supervisor drop down, it does not set the values of the other textboxes from what got returned from the database.
public ActionResult NewAccount(int? supervisorAccountId)
{
Account account = new Account();
ViewData.Model = account;
ViewData["Supervisor"] = from ac in _accountService.GetByIsManager(true).OrderBy(ac => ac.FirstName).ToList()
select new { AccountId = ac.AccountId, Name = ac.FirstName + " " + ac.LastName };
In your view you need to pass the values in the HtmlHelper.TextBox method call.You can get to the properties of your Account object in the view by using the syntax "Model."
Did you think about using jquery to implement your requirement?.
If you were interested in it , you could take a look at the following example. The following example gets regions JSON data from server and add mouseover and mouseout event for each region item. When the mouse is over on an region item, the corresponding
attractions data will be requested by using Jquery Ajax. You can add a "onchange" event to the "dropdownlist" you mentioned. Then you can get json data from the method by using ajax and assign value to each textbox.
e.g.
<script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
<script language="JavaScript" type="text/javascript">
$(document).ready(function() {
$.ajax({
type: "post",
url: "/Shared/ReturnRegion",
dataType: "json",
success: function(data) { addEvent(data); }
});
});
function addEvent(data) {
region = data;
for (var i = 0; i < data.length; i++) {
var id = "Region_" + data[i].RegionId;
strRegion = data[i].RegionId + "_";
$("#" + id).mouseover(function() { aMouseOver(this); }); //
$("#" + id).mouseout(function() {
setTimeout(hiddenDiv, 1000);
});
}
}
function aMouseOver(o) {
var id = o.id;
var region = id.substring(id.lastIndexOf('_') + 1, id.length);
$.ajax({
data: "id=" + region,
type: "post",
url: "/Shared/ReturnAttractionsById",
dataType: "json",
success: function(result) { addMenuItems(result, o); }
});
}
</script>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using ATRInforSite.Models;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace ATRInforSite.Controllers
{
public class SharedController : Controller
{
ATRInforEntities atr = new ATRInforEntities();
public ActionResult Index()
{
return View();
}
public ActionResult ReturnRegion()
{
var result = from r in atr.RegionSet where r.IsValid == true select new { RegionId = r.Id };
return Json(result);
}
public ActionResult ReturnAttractionsById(int id)
{
var result = from a in atr.AttractionSet
where (a.IsValid == true && a.Region.Id == id)
orderby a.DisplayOrder ascending
select new { Id = a.Id, Name = a.AttractionName };//, RegionId = a.Region.Id
return Json(result);
}
}
}
CodeHobo
Star
13537 Points
1961 Posts
Re: ASP.NET MVC DropdownList
Nov 28, 2009 05:39 PM|LINK
No problem, glad it helped.
Can you post the complete javascript block from the view? That error normally indicates that an operation is trying to be performed on an undefined object.
Blog | Twitter : @Hattan
sillysoumare
Member
17 Points
61 Posts
Re: ASP.NET MVC DropdownList
Nov 28, 2009 06:14 PM|LINK
<script language="javascript" type="text/javascript">
$(document).ready(function()
{
$("#Supervisor").change(function()
{
var supervisorId = $("#Supervisor").val();
$("#AccountForm").load("/Account/NewAccount/" + supervisorId);
});
});
</script>
public ActionResult NewAccount(int? supervisorId)
{
Account account = new Account();
ViewData.Model = account;
ViewData["Supervisor"] = from ac in _accountService.GetAll().Where(ac => ac.IsManager == true).OrderBy(ac => ac.FirstName).ToList()
select new { AccountId = ac.AccountId, Name = ac.FirstName + " " + ac.LastName };
if (supervisorId != null)
{
}
return View("HRAddAccount");
}
This view has some other textboxes that get their values set after the user selects a supervisor, it will go back to the controller and makes a call to the database
then get the values for the remaining textboxes wich are read only then redisplay the NewAccount view.
CodeHobo
Star
13537 Points
1961 Posts
Re: ASP.NET MVC DropdownList
Nov 28, 2009 07:53 PM|LINK
Is there any javascript in HRAddAccount? I'm not seeing the javascript usage of settings. where in the code do you reference "settings" ?
Blog | Twitter : @Hattan
sillysoumare
Member
17 Points
61 Posts
Re: ASP.NET MVC DropdownList
Nov 29, 2009 12:08 AM|LINK
You are right, I got it working now after I removed another javascript that was refering to in the Master Page.
However, I got another problem. When I render the same view after the user selects an item from the Supervisor drop down, it does not set the values of the other textboxes from what got returned from the database.
public ActionResult NewAccount(int? supervisorAccountId)
{
Account account = new Account();
ViewData.Model = account;
ViewData["Supervisor"] = from ac in _accountService.GetByIsManager(true).OrderBy(ac => ac.FirstName).ToList()
select new { AccountId = ac.AccountId, Name = ac.FirstName + " " + ac.LastName };
if (supervisorAccountId != null)
{
Account supervisorAccount = _accountService.GetById(Convert.ToInt32(supervisorAccountId));
account.DivisionDescription = supervisorAccount.DivisionDescription;
account.DepartmentDescription = supervisorAccount.DepartmentDescription;
}
return View("HRAddAccount", account);
}
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Domain.Account>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="Head" runat="server">
<script language="javascript" type="text/javascript">
$(document).ready(function() {
$("#Supervisor").change(function() {
var supervisorAccountId = $("#Supervisor").val();
$("#AccountForm").load("/Account/NewAccount/" + supervisorAccountId);
});
});
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="TitleContent" runat="server">
Add Account
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server">
<% using (Html.BeginForm<AccountController>(ac => ac.AddAccount(), FormMethod.Post)) {%>
<fieldset>
<legend>Add Account</legend>
<table>
<tr>
<td>Supervisor:</td>
<td><%=Html.DropDownList("Supervisor", new SelectList((IEnumerable)ViewData["Supervisor"], "AccountId", "Name", " "), new { style = "width:155px" })%></td>
<td>Division Description:</td>
<td><%= Html.TextBox("DivisionDescription", null, new { ReadOnly = "true", MaxLength = "50" })%></td>
</tr>
<tr>
<td>Department Description:</td>
<td><%= Html.TextBox("DepartmentDescription", null, new { ReadOnly = "true", MaxLength = "50" })%></td>
</tr>
</table>
</fieldset>
<% } %>
</asp:Content>
CodeHobo
Star
13537 Points
1961 Posts
Re: ASP.NET MVC DropdownList
Nov 29, 2009 01:14 AM|LINK
In your view you need to pass the values in the HtmlHelper.TextBox method call.You can get to the properties of your Account object in the view by using the syntax "Model."
So something like this:
<td><%= Html.TextBox("DivisionDescription", Model.DivisionDescription, new { ReadOnly = "true", MaxLength = "50" })%></td> ... <td><%= Html.TextBox("DepartmentDescription", Model.DepartmentDescription , new { ReadOnly = "true", MaxLength = "50" })%></td>You were passing null before and that tells the helper to display a blank textbox.
Blog | Twitter : @Hattan
sillysoumare
Member
17 Points
61 Posts
Re: ASP.NET MVC DropdownList
Nov 29, 2009 04:59 PM|LINK
I did this
<td><%= Html.TextBox("DivisionDescription", Model.DivisionDescription, new { Disabled = "true", MaxLength = "50" })%></td>
but after the postback I can see the value is being set in the controller, however it does not reflect on the view
KeFang Chen...
Star
8329 Points
852 Posts
Microsoft
Re: ASP.NET MVC DropdownList
Nov 30, 2009 07:16 AM|LINK
Hi,
Did you think about using jquery to implement your requirement?.
If you were interested in it , you could take a look at the following example. The following example gets regions JSON data from server and add mouseover and mouseout event for each region item. When the mouse is over on an region item, the corresponding attractions data will be requested by using Jquery Ajax. You can add a "onchange" event to the "dropdownlist" you mentioned. Then you can get json data from the method by using ajax and assign value to each textbox.
e.g.
<script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script> <script language="JavaScript" type="text/javascript"> $(document).ready(function() { $.ajax({ type: "post", url: "/Shared/ReturnRegion", dataType: "json", success: function(data) { addEvent(data); } }); }); function addEvent(data) { region = data; for (var i = 0; i < data.length; i++) { var id = "Region_" + data[i].RegionId; strRegion = data[i].RegionId + "_"; $("#" + id).mouseover(function() { aMouseOver(this); }); // $("#" + id).mouseout(function() { setTimeout(hiddenDiv, 1000); }); } } function aMouseOver(o) { var id = o.id; var region = id.substring(id.lastIndexOf('_') + 1, id.length); $.ajax({ data: "id=" + region, type: "post", url: "/Shared/ReturnAttractionsById", dataType: "json", success: function(result) { addMenuItems(result, o); } }); } </script> using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Mvc.Ajax; using ATRInforSite.Models; using System.IO; using System.Data; using System.Data.SqlClient; using System.Configuration; namespace ATRInforSite.Controllers { public class SharedController : Controller { ATRInforEntities atr = new ATRInforEntities(); public ActionResult Index() { return View(); } public ActionResult ReturnRegion() { var result = from r in atr.RegionSet where r.IsValid == true select new { RegionId = r.Id }; return Json(result); } public ActionResult ReturnAttractionsById(int id) { var result = from a in atr.AttractionSet where (a.IsValid == true && a.Region.Id == id) orderby a.DisplayOrder ascending select new { Id = a.Id, Name = a.AttractionName };//, RegionId = a.Region.Id return Json(result); } } }