dropdown in mvc3 edit form - c#

This maybe very simple but I cant seem to sort it out on my own.
I have created a simple db and entity modal that looks like this
I am trying to create an Create form that allows me to add a new Order. I have a total of 3 tables so what I am trying to do is have the form allowing the person to enter Order date and also has a dropdown list that allows me to select a product from the product table
I want to be able to create a Add or Edit view that allow me to insert the OrderDate into the OrderTable and also insert the OrderID and selected ProductID into OrderProduct.
What steps do I need to do here.
I have created an OrderController and ticked the "Add Actions" and than added a Create View which looks like this
#model Test.OrderProduct
#{
ViewBag.Title = "Create2";
}
<h2>Create2</h2>
<script src="#Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="#Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
#using (Html.BeginForm()) {
#Html.ValidationSummary(true)
<fieldset>
<legend>OrderProduct</legend>
<div class="editor-label">
#Html.LabelFor(model => model.OrderID)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.OrderID)
#Html.ValidationMessageFor(model => model.OrderID)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.ProductID)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.ProductID)
#Html.ValidationMessageFor(model => model.ProductID)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>
This creates the view that contains a textbox for both OrderID and ProductID however no date.
My controller CreatePost hasnt been changed
[HttpPost]
public ActionResult Create(FormCollection collection)
{
try
{
var data = collection;
// TODO: Add insert logic here
// db.Orders.AddObject(collection);
return RedirectToAction("Index");
}
catch
{
return View();
}
}
My questions are,
1.How do I swap out ProductID textbox to be a dropdown which is populated from Product
2.How do I get the data from FormCollection collection? I thought of just a foreach however I dont know how to get the strongly typed name
Any help for a newbie would be very helpful.
Thank you!

First thing's first, don't bind to the Order entity. Never bind to an EF object, always try and use a ViewModel. Makes life simpler for the View, and that is the goal here.
So, have a ViewModel like this:
public class CreateOrderViewModel
{
public int OrderId { get; set; }
public DateTime OrderDate { get; set; }
public int SelectedProductId { get; set; }
public IEnumerable<SelectListItem> Products { get; set; }
}
That's it right now.
Return that to your View in your [HttpGet] controller action:
[HttpGet]
public ActionResult Create()
{
var model = new CreateOrderViewModel
{
Products = db.Products
.ToList() // this will fire a query, basically SELECT * FROM Products
.Select(x => new SelectListItem
{
Text = x.ProductName,
Value = x.ProductId
});
};
return View(model);
}
Then to render out the list of Products: (basic HTML excluded)
#model WebApplication.Models.CreateOrderViewModel
#Html.DropDownListFor(model => model.SelectedProductId, Model.Products)
The only thing i don't know how to do is bind to the DateTime field. I'm guessing you would need an extension method (HTML Helper) which renders out a Date Picker or something. For this View (creating a new order), just default to DateTime.Now.
Now, onto the [HttpPost] controller action:
[HttpPost]
public ActionResult Create(CreateOrderViewModel model)
{
try
{
// TODO: this manual stitching should be replaced with AutoMapper
var newOrder = new Order
{
OrderDate = DateTime.Now,
OrderProduct = new OrderProduct
{
ProductId = SelectedProductId
}
};
db.Orders.AddObject(newOrder);
return RedirectToAction("Index");
}
catch
{
return View();
}
}
Now, i also think your EF model needs work.
To me (in English terms), a Product can have many orders, and an Order can have many Products.
So, it should be a many-to-many. Currently it's a 1-1 with a redundant join table. Did you generate that from a DB? If so, your DB possibly needs work.
You should have a navigational property called Products on the Order entity, which references a collection of Product, made possible by a silent join to the join table in the many-to-many.
This also means you no longer have a DropDownList, but a MultiSelectDropDownList.

Thanks Craig. Your few days (as at time of posting) of MVC have solved my few days of trying to get the selected value back from DropDownListFor.
I had no problem in the Create view in getting the selected value of the DDLF, but the Edit view was a completely different matter - nothing I tried would get the selected value back in the Post. I noticed the selected value was lurking in the AttemptedValue of the ModelState, and so Dr.Google referred me here.
I had this in my view
#Html.DropDownList(model => model.ContentKeyID, Model.ContentKeys, Model.ContentKeyName)
where ContentKeys is a SelectList populated from the DB via a ViewModel, and ContentKeyName is the curently selected name.
The wierd thing is, I have another DDL on the view populated in an identical manner. This one works. Why, I don't know. It is the second DDL on the form, but I can't see that making a difference.
I read somewhere else it might have been that I was using Guids as the Id, but that didn't seem to make a difference - I changed to Int32, but don't think I had to - I think it's enums that disagree with DDLF. Nullables seemd to make no difference either.
Now that I've added the form collection to my Post ActionResult, and get the selected value using
-view
#Html.DropDownList("ContentKey", Model.ContentKeys)
-in controller (Post)
contentKeyId = int.Parse(form.GetValue("ContentKey").AttemptedValue);
all is good, and I can get on with more exciting things. Why is that the simplest things can hold you up for so long?

I have been struggling with this over the last day or so too. I'll share my limited knowledge in the hope that it will help someone else.
Note that I use a singleton with a pre-populated list to keep my example application small (i.e. no EF or DB interaction).
To show the ProductList you will need to have a ViewBag or ViewData item which contains the ProductList.
You can set this up in the Controller with something like
ViewData["ProductList"] = new SelectList(Models.MVCProduct.Instance.ProductList, "Id", "Name", 1);
Then update your View to something like:
<div class="editor-field">#Html.DropDownList("ProductList")</div>
For the Post/Create/Update step you need to look into the FormCollection to get your results. Reading other posts it sounds like there used to be a bug in here, but it's fixed now so ensure you have the latest. For my example I have a DropDownList for Product so I just get the selected Id and then go searching for that Product in my list.
[HttpPost]
public ActionResult Create(FormCollection form )//Models.MVCOrder newOrder)
{
MVC.Models.MVCOrder ord = Models.MVCOrder.Instance.CreateBlankOrder();
//Update order with simple types (e.g. Quantity)
if (TryUpdateModel<MVC.Models.MVCOrder>(ord, form.ToValueProvider()))
{
ord.Product = Models.MVCProduct.Instance.ProductList.Find(p => p.Id == int.Parse(form.GetValue("ProductList").AttemptedValue));
ord.Attribute = Models.MVCAttribute.Instance.AttributeList.Find(a => a.Id == int.Parse(form.GetValue("attributeId").AttemptedValue));
UpdateModel(ord);
return RedirectToAction("Index");
}
else
{
return View(ord);
}
}
I've only been working on MVC3 for the last few days, so any advice on improving the above would be appreciated.

Related

Passing back a single object when using EditorFor(model[])

This has been a thorn in my side for a while. If I use EditorFor on an array of objects and the editor Template has a form in it ex.
public class FooController:Controller {
public ActionResult Action(Foo foo) {
// ...
}
}
Index.cshtml
#model IEnumerable<Foo>
#Html.EditorFor(m=> m)
EditorTemplate
#model Foo
#using (Html.BeginForm("action", "controller"))
{
#Html.TextBoxFor(f=> f.A)
#Html.CheckBoxFor(f=> f.B)
#Html.LabelFor(f=> f.B)
}
So I'll hit a few problems.
The checkbox label's for doesn't bind correctly to the checkbox (This has to do with the label not receiving the proper name of the property ([0].A as opposed to A).
I'm aware I can get rid of the pre- text by doing a foreach on the model in Index but that screws up ids and naming as the framework doesnt realize there are multiples of the same item and give them the same names.
For the checkboxes I've just been doing it manually as such.
#Html.CheckBoxFor(m => m.A, new {id= Html.NameFor(m => m.A)})
<label for="#Html.NameFor(m => m.A)">A</label>
However I cant solve the inability of the controller to accept the item as a single model. I've even tried allowing an array of Foo's in the Action parameters but that only work when its the first item being edited ([0]...) if its any other item in the array (ex. [1].A) the controller doesn't know how to parse it. Any help would be appreciated.
Make your model a class with the properties you need.
create a class in your Models subfolder
public class MyModel {
public IEnumerable<Foo> Foolist { get ; set;}
public string Something { get;set;}
}
your EditorFor will have to have a foreach loop for Foolist...
MVC will attempt to put your model together from the form and return it to your POST action in the controller.
Edit:
You could create an EditorTemplate for foo. In Views/Shared/EditorTemplates folder, create FooTemplate.cs
#model Foo
<div class="span6 float-left" style="margin-bottom: 6px">
#Html.TextBoxFor(m => m.A, new { style = "width:190px" })
#Html.CheckBoxFor(m => m.B, new { style = "width:40px" })
#Html.ValidationMessage("foo", null, new { #class = "help-inline" })
</div>
then in your view
#foreach (var myFoo in Model)
{
#EditorFor(myFoo)
}
This still suffers from the "model gets passed back as a whole" requiredment of yours. Not sure about why there is a need to process these individually.
Hah finally solved this - Here's how I did it. As a bit of background HTML forms use the name attribute when submitting forms, but the label for element uses Id . so I only adapt the id tag to have the prefix and not the name tag.
--In the cshtml file
#{
var prefix = ViewData.TemplateInfo.HtmlFieldPrefix;
ViewData.TemplateInfo.HtmlFieldPrefix = "";
}
then I can specify the id for the properties by their prefix while letting the name remain the same like so
#Html.CheckBoxFor(m => m.A,
new {id = prefix+"."+ Html.NameFor(m => m.A)})
<label for="#prefix.#Html.NameFor(m => m.A)">A!</label></div>

Query Selected Items using checkbox in MVC

I like to ask if how would it be possible that the selected items in the checkbox will be used in making linq query in MVC.
I have this one in my view where in I displayed all the possible options in which the user will just simply select the types of softwares that will be used to generate reports.
#using (Ajax.BeginForm("Create_Report", "Softwares",
new AjaxOptions
{
HttpMethod = "POST",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "target2"
}))
{
#Html.ValidationSummary(true)
<p>For A Reports: Kindly check softwares to create reports.</p><br />
foreach (var item in Model) {
<input type="checkbox" value="#item.software_name" name="software_type"/>#item.software_name
<br />
}
<input type="submit" value="Create Report"/>
}
After that, I want that the selected software types will be used in the query like for example if the user selects Adobe Pro, Adobe Illustrator, MS Visio, and Acrobat, the query should go like "Select from Software _table where software__type = "Adobe Pro" && software_type ="Adobe Illustrator && "so fort.
Is there any ways to shorten the query using the selected items from the checkbox? Any help is much appreciated.
Assuming your POST method is
[HttpPost]
public ActionResult Create_Report(string[] software_type)
then software_type will contain an array of the selected values so you can use .Contains() to filter the query
var items = db.Software.Where(s => software_type.Contains(s.software_type))
Side note: remove those awful underscores from your properties and follow normal naming conventions (SoftwareType, SoftwareName, etc or simply Type, Name etc since they are already in a class named Software)
You need to define a collection to store you selected items and use model binding to map the selected items to this property.
//store checked Items
public IEnumerable<string> SelectedSoftware { get; set; }
//check box list items
public List<string> SoftwareList = new List<string>();
foreach (var item in Model.SoftwareList )
{
<input type="checkbox" name="SelectedSoftware" value="#item">#item
}
in your controller:
public ActionResult Create_Report(string[] SelectedSoftware)
{
//do action
}

MVC (Razor) Filter DropDownList

I'm new to MVC and JavaScript and I have a question about DropDownList.
I Build a program of "Car Renting" and I have a View that allow adding new Car to the inventory.
The view include 2 dropdownlists (see below):
<div class="editor-field">
#Html.DropDownList("ManufacturerId", string.Empty)
#Html.ValidationMessageFor(model => model.ManufacturerId)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.ModelId)
</div>
<div class="editor-field">
#Html.DropDownList("ModelId", string.Empty)
#Html.ValidationMessageFor(model => model.ModelId)
</div>
I want that when user select a manufacturer for example (Alfa Romeo) the "model" list box will display only "Alfa Mito" models and not the full list...
My Controller function send to the view the Model List using ViewBag see below:
public ActionResult AddNewCar()
{
ViewBag.ManufacturerId = new SelectList(db.Manufacturers, "ManufacturerId", "ManufacturerName");
ViewBag.ModelId = new SelectList(db.Models, "ModelId", "ModelName");
ViewBag.BranchId = new SelectList(db.Branchs, "BranchId", "BranchName");
return View();
}
Please advice.
Thanks,
Almog
call below action method from jquery on dropdown change event.
public List<carmodels> PopulateState(int manufacturerId)
{
var carmodelsObj = (from st in dc.Carmodels
where st.manufacturerId.Equals(manufacturerId)
select st).ToList();
return (carmodelsObj);
}
You need to repopulate/populate your ModelId dropdown on every time when your first dropdown changes (using jquery). Do following:
Get the ManufactureId from first dropdown on change event. and place an ajax call to repopulate your second dropdown.
$("#ManufactureId").change(function() {
var manufacturerId = manufacturer$('#ManufacturerId').val();
$.ajax({url: "~/Car/GetCarsByManufacturer", success: function(result){
//re populate your second dropdown here
//hint: you may use response.id for value
}});
});
Create a function in your controller that returns Car Models, based on selected. (call this function in previous step using ajax).
public List<carmodels> GetCarsByManufacturer(int manufacturerId)
{
var carmodelsObj = (from st in dc.Carmodels
where st.manufacturerId.Equals(manufacturerId)
select st).ToList();
return (carmodelsObj);
}

ASP.NET MVC CheckBoxList from model with List Property

Apologies if the title is unclear.
I'm trying to return my model from a form submit in ASP.NET MVC.
My question is nearly the same as this question, only differing in that I don't have a List<Model> but a model like:
public Model
{
string UserName {get; set;}
string Password {get; set;}
List<Roles> UserRoles {get; set;}
}
where I need the UserRoles as checkboxes that the admin can select from when creating a new user. My question is, I'm unsure how to use a '#Html.CheckBoxFor' against a list. I tried this:
#for (var i = 0; i < Model.UserRoles.Count();i++ )
{
#Html.HiddenFor(model => model.UserRoles[i].RoleID)
#Html.CheckBoxFor(model => model.UserRoles[i].Selected)
#Html.LabelFor(model => model.UserRoles[i].Name)
}
which in no way worked - every label on the page is "Name", and my List was empty in the POST. Can anyone offer me any guidance on this?
No need to go away from Razor at all.
This works for me:
for (var i = 0; i < Model.UserRoles.Count(); i++)
{
var role = Model.UserRoles[i];
#Html.HiddenFor(model => model.UserRoles[i].RoleId)
#Html.CheckBoxFor(model => model.UserRoles[i].Selected)
#Html.LabelFor(model=> model.UserRoles[i].Name, role.Name)
}
See below code, this way you don't need to hide the role Id, also when you save the selected roles for the user, you don't need to loop through all roles to see which role is selected.
View
#foreach (Roles info in Model.UserRoles)
{
<span>
<input type="checkbox" class="checkbox" name="selectedRoles" value="#info.RoleName" id="#infoRoleName" />
<label for="#info.RoleName">#info.RoleName</label>
</span>
}
Action
[HttpPost]
public ActionResult CreateUsers(Model model, string[] selectedRoles)
{
//
}
From your code in the view, the post should work fine providing your post action looks like this:
[HttpPost]
public ActionResult Action(Model model)
{
return View(model);
}
i.e. passing your model across as the argument.
Also make sure you have your model reference in the view too:
#model YourNameSpace.Model
Post a list of check boxes to server and get list of checked items
linq left join to check whether checked, generating checkboxes,received checked list
View
List<eDurar.Models.tbl_ISOCetificate> ModList = db.tbl_ISOCetificate.ToList();
var li = (from cert in db.tbl_ISOCetificate join comCert in db.tbl_CompCertificate on cert.Cert_id equals comCert.CompCer_id into jo from b in jo.DefaultIfEmpty()
select new {cert.Cert_id,cert.Cert_Name,chkd = b.CompCer_SerId==null?"":"checked"}).ToList();
foreach (var item in li)
{
#:<div style="width: 30%; display: inline-block; margin: 1em">
#:<input type="checkbox" #item.chkd name="CheckedCertificates" value="#item.Cert_id">
#:<label>#item.Cert_Name</label>
#:</div>
}
Controller
[HttpPost]
public ActionResult ManageSurveyGroup(int[] CheckedCertificates)
{
return View();
}

C# MVC Query with WHERE clause and 2 params from the View

I need to query my database with a WHERE clause and pass to parameters from my view to the controller. I am at a total loss as I am a JavaScript and PHP developer and am currently learning dotnet MVC (I am using vs10 framework4).
I need to in essence make this query:
select * from myTable where column1=16 and column2=24
The values of column1 and column2 are in my View. I have found a million examples on how to pull either a whole table, or a result based upon one parameter, I cannot figure out how to do that simple query.
This seems like a simple task and I would appreciate any help as I have spent 5 hours trying to figure this out.
Here are some key components of my code that will hopefully help someone help me. I sincerely appreciate any help.
Controller:
public class SignBuilderController : Controller
{
SignBuilderModel signBuilderModel = new SignBuilderModel();
//
// Initialize database entities
SignBuilderEntities _db;
public SignBuilderController()
{
_db = new SignBuilderEntities();
}
//
// Get /SignBuilder/PrintSetup
[Authorize]
public ActionResult PrintSetup()
{
var pricesList = signBuilderModel.GetPricingList().ToList();
return View("PrintSetup", pricesList);
}
// get Column1 and column 2 query
public ActionResult TestPage(int column1, int column1)
{
//do something here
return View();
}
And in my View I would retrieve the values for my where clause from input fields.
Example:
<input type="text" name="column1value" id="column1value" />
<input type="text" name="column2value" id="column2value" />
Obviously I am using a Model as well, so if that is needed to make this work no problem. I am really looking for sample code I can use as an example to learn from. I really appreciate any help, and am about to pull out my hair.
Try this(I assume your view is strongly typed):
public ActionResult(int column1, int column2)
{
//do something here.
var model =
(
from p in this.GetPricingList()
where (p.column1 == column1) && (p.column2 == column2)
select p
).FirstOrDefault();
return View(model);
}
and in the View:
<input type="text" name="column1" id="column1value" value="<%=Model.column1%>"/>
<input type="text" name="column2" id="column2value" value="<%=Model.column2%>"/>
You need to post the values to the Action method on your controller
Using something like this in your view:
<% using (Html.BeginForm("RunQuery", "ControllerName")) { %>
<input type="text" name="column1" id="column1value" />
<input type="text" name="column2" id="column2value" />
<input type="submit" value="Run Query" />
<% } %>
and then the accompanying action method
[HttpPost]
public ActionResult RunQuery(string column1, string column2)
{
var results = GetDataFromDatabase(column1, column2);
return View(results );
}
I'm afraid your code would not compile. What you annotated as your controller does not inherit from controller class at all and your actionresult method has no name. I wonder how could you visit your page in the browser. The code should be like
public class MyApplicationModel:Controller
{
//use entity framework
private MyApplicationEntities entities = new MyApplicationEntities();
//
// Method to query database for price lists table
public IQueryable<pricingUploaded> GetPricingList()
{
return entities.pricingUploadeds;
}
//
// Method to query Column1 and Column2 in table pricingUploaded
[HttpGet]
public ActionResult Query()
{
//this action will render the page the first time
return View();
}
[HttpPost]//this method will only accept posted requests
public ActionResult Query(int column1, int column2)
{
//do something here.
var _result = entities.Where(x=>x.column1 == column1 && x.column2 == column2);
return View(_result);//pass result to strongly typed view
}
In your view you have to create a form, that when submitted can post values to HttpPost overload of Query method
<%:Html.BeginForm();%>
<input type="text" name="column1">
<input type="text" name="column2">
<input type="submit" id="save" value="query">
<%:Html.EndForm();%>
Now you enter values in your form and click query button, you will have column1 and column2 values in you Query Actionresult accepting posted request. You better put a break point there and inspect how things are really working and figure out how to do what you need to do.
Edit: based on your comment that you want to pass values ajaxically you can do little bit of jQuery:
<script type="text/javascript">
$(function(){
$("form").submit(function(){
$.ajax({
type:"post",
url:"myapplicationmodel/query",
success:function(data)
{
//do something with returned data
}
});
return false;// to prevent normal form post
});
});
</script>
What about this:
public ActionResult(int column1, int column2)
{
var query = from m in _db.MyTable
where m.Column1 == column1
where m.Column2 == column2
select m;
// Or this for typed model
// select new MyModel { Column1 = m.Column1, etc }
var model = query.First();
return View(model);
}
I'm assuming that the entities is a Linq to Entity right? if that is the case, couldn't you use:
var model = from e in entities
where column1=16 and column2=24
select e;
and then pass that model into the vew.
Basically, if entities is of the type IQueryable or IEnumerable or any of the other linq-able interfaces, you can perform that type of query. Make sure that you are using System.Linq; in the top of your code file.
Hope that helps!

Categories

Resources