dropdownlist data is not posted to server - asp.net-mvc-3

I have a dropdownlist which is populated with data from ViewBag. The code looks like
connectionString str=new connectionString();
ViewBag.Dept=str.Department;
Now here Department is a table.
I have a dropdownlist
#Html.DropDownList("Department", new SelectList(ViewBag.Dept, "Id", "Id"), "Select Department")
The Action Method is
public ActionResult Edit(Emp emp)
{
if (ModelState.IsValid)
{
db.Emps.AddObject(emp);
return RedirectToAction("Details");
}
return View();
}
within browser the dropdownlist is showing data from database and when I try to post the form to the server the value is null in case of dropdownlist . The above code is fine with MVC4 but problem with MVC3. Please help me

Related

Cannot Populate TextBox in MVC

public IActionResult Create() { ViewData["StateId"] = new SelectList(_context.States, "Id", "Abbreviation")
ViewData["UserId"] = Guid.NewGuid().ToString(); return View(); }
I have the above code where I need to populate a dropdown list with some States e.g. Alabama etc.
Then I need to populate a textbox with a UserID(new Guid). This code is in my controller in the part that a new user is created. The code populates the dropdown list but not the textbox.
Any idea why?

finding out the firstrow values of <SelectListItem> in the controller before sending them to view MVC3?

Im binding UsersList to a dropdownlist using a selectlistitem
#Html.DropDownListFor(m => m.UserId, new SelectList(Model.Users, "Value", "Text", Model.UserId))
public List<SelectListItem> Users { get; set; }
Public ActionResult LoadUsers()
{
UserModel usrModel=new UserModel();
usrModel.LoadUsers();
string firstUser=?
return view(usrModel);
}
Im binding UsersList to a dropdownlist using a selectlistitem
my requirement is when the user is sensing the Users to view from controller, i want to find out the value field of the User in first row, i.e before binding to the dropdownlist itself i want to find out who is the first User who will be selected in the dropdownlist when the page first loads

How to update database by attaching model to DataContext

I am building a web site using MVC 3 Framework.I have a Linq to SQL DataContext to handle data access.According to what I've learned you can use a post method to get the form information when a user submits a form and MVC frmaework can map the data to a model object.
something like this :
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(Entity model)
But I have problem with attaching this model object to the data context object here's what I am doing :
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(Entity model)
{
if(ModelState.IsValid)
{
_dataContext.Attach(model);
_dataContext.SubmitChanges();
return RedirectToAction("Index");
}
return View("Edit", model);
}
But nothing happens.What am I doing wrong ?
The problem is that your _dataContext doesn't know that your entity has been changed.
So you would have to use the overload that sets modified as true:
_dataContext.Attach(model, true);

Asp.net mvc Creating a new record

I've been trying to add a new record.
public ActionResult Create()
{
var dc = new ServicesDataContext();
ViewData["CustomerID"] = TempData["CustomerID"];
var a = dc.services.Select(arg => arg.ServiceID).ToList();
ViewData["ServiceID"] = new SelectList(a);
var model = new Maping();
return View(model);
}
//
// POST: /Customerservice/Create
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create([Bind(Exclude ="CustomerServiceMappingID")] Maping serviceToCreate)
{
if (!ModelState.IsValid)
return View();
var dc = new ServicesDataContext();
dc.Mapings.InsertOnSubmit(serviceToCreate);
dc.SubmitChanges();
return RedirectToAction("Index", "Home");
}
Now the situation is that the tempdata has the correct value but by the time i submit changes the customerID turns out to be null. So, kindly help me in solving this.
AFAI understand you copy the customer id from TempData to ViewData. However, the contents of the ViewData will be not preserved after the request ends. In your view you should put the customer id into an input (e.g. a hidden field if it should be not displayed) to get it back in your post action.
If you use a strongly typed model, you should not use the ViewData at all, but rather you should set the customer id on the model instance. Then in the view you could use a Html.HiddenFor(m => m.CustomerId) to "preserve" this id.
I hope I did not misunderstand the question, unfortunately it is not really visible in your code snippets where you would have a customer id in the post action that is null.

Dropdownlist value not getting set from SelectList passed to view in ViewData

I am trying to populate a dropdownlist of office locations(text) and addresses (value)
When viewing my page source in my browser after displaying the page I can see that the select (dropdownlist) option values are all "". Here is my code. I am using a LinqToSql data context call to get my data for the SelectList. In the debugger I can see that the list returned from my call does in fact have the addresses and office locations I need.
My model datacontext code:
public partial class uls_dbDataContext
{
public List<office_location> GetOfficeLocations()
{
return office_locations.ToList();
}
}
My controller code:
public ActionResult Directions()
{
uls_dbDataContext ulsdb_dc = new uls_dbDataContext();
ViewData["OfficeLocations"] = new SelectList(ulsdb_dc.GetOfficeLocations(),"location_address", "location_name");
ViewData["Title"] = "Directions";
return View();
}
My view code:
<%= Html.DropDownList("locations", (SelectList)ViewData["OfficeLocations"])%>
I had to upgrade to MVC RC2 from preview 4. It addressed drodownlist issues and fixed my problem.

Resources