Unable to get SelectList working in ASP.NET MVC - html

I am trying to get my SelectList working. I tried adding the options in the model class, but I'm unable to get that working with an error
Member Testing.TaxIDType cannot be accessed with an instance reference; qualify it with a type name instead
which I am not sure about how to fix.
I added the SelectList items to my model:
public static List<SelectListItem> TaxIDType = new List<SelectListItem>()
{
new SelectListItem() { Text = "FEIN", Value = "FEIN" },
new SelectListItem() { Text = "SSN", Value = "SSN" }
};
I try to call the in the view using the following statement.
#Html.DropDownListFor(Model => Model.TaxIDType, class.TaxIDType)
I am getting the following two errors:
'IEnumerable' does not contain a definition for 'TaxIDType' and no accessible extension method 'TaxIDType' accepting a first argument of type 'IEnumerable' could be found (are you missing a using directive or an assembly reference?)
Member 'Testing.TaxIDType' cannot be accessed with an instance reference; qualify it with a type name instead
Can someone help me with this?

Here is a step by step explanation of using DropDownListFor helper in your case.
Create a model which will contain a collection of SelectListItem instances.
public class MyModel
{
public List<SelectListItem> TaxIDType = new List<SelectListItem>()
{
new SelectListItem() { Text = "FEIN", Value = "FEIN" },
new SelectListItem() { Text = "SSN", Value = "SSN" }
};
}
Note: if you want, you can even make the property static, however, you will need to change the way you reference the property on the view (will explain it eventually)
Create the model and pass it to the view.
public ActionResult Index()
{
var viewModel = new MyModel();
return View(viewModel);
}
In the view, state that your view uses MyModel class. You can do it by putting the #model helper on the top of your view (.cshtml file).
#model WebApplication3.Controllers.MyModel
Here, WebApplication3.Controllers is a namespace of your model and MyModel is it's name. Don't use the listed namespace and model, but change them to your own.
Use the DropDownListFor helper as following
#Html.DropDownListFor(x => ??, Model.TaxIDType)
In the example, Model is a property which is being populated by the framework to let your acecss a class instance which you passes in View() method inside the controller. (MyModel instance in this case).
By writing Model.TaxIDType you are referencing the property which contains the list of items to poppulate the dropdown.
Notice, that the property in the model isn't marked as static, that's why we have to access it via the instance (Model) reference. However, you can, indeed, make the property static. In that case, you will need to change the usage in the view to the following:
#Html.DropDownListFor(x => ??, MyModel.TaxIDType)
The last thing to mention: take a look at the question marks. In order to get the selected value back, you will need to map it to the model. One way to do it is to extend the model.
public class MyModel
{
public string SelectedTaxIdType { get; set; }
public List<SelectListItem> TaxIDType = new List<SelectListItem>()
{
new SelectListItem() { Text = "FEIN", Value = "FEIN" },
new SelectListItem() { Text = "SSN", Value = "SSN" }
};
}
and than modify the view
#Html.DropDownListFor(x => x.SelectedTaxIdType, Model.TaxIDType)

Related

ASP .NET MVC Drop Down List throwing multiple errors when empty [duplicate]

I have the following view model
public class ProjectVM
{
....
[Display(Name = "Category")]
[Required(ErrorMessage = "Please select a category")]
public int CategoryID { get; set; }
public IEnumerable<SelectListItem> CategoryList { get; set; }
....
}
and the following controller method to create a new Project and assign a Category
public ActionResult Create()
{
ProjectVM model = new ProjectVM
{
CategoryList = new SelectList(db.Categories, "ID", "Name")
}
return View(model);
}
public ActionResult Create(ProjectVM model)
{
if (!ModelState.IsValid)
{
return View(model);
}
// Save and redirect
}
and in the view
#model ProjectVM
....
#using (Html.BeginForm())
{
....
#Html.LabelFor(m => m.CategoryID)
#Html.DropDownListFor(m => m.CategoryID, Model.CategoryList, "-Please select-")
#Html.ValidationMessageFor(m => m.CategoryID)
....
<input type="submit" value="Create" />
}
The view displays correctly but when submitting the form, I get the following error message
InvalidOperationException: The ViewData item that has the key 'CategoryID' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'.
The same error occurs using the #Html.DropDownList() method, and if I pass the SelectList using a ViewBag or ViewData.
The error means that the value of CategoryList is null (and as a result the DropDownListFor() method expects that the first parameter is of type IEnumerable<SelectListItem>).
You are not generating an input for each property of each SelectListItem in CategoryList (and nor should you) so no values for the SelectList are posted to the controller method, and therefore the value of model.CategoryList in the POST method is null. If you return the view, you must first reassign the value of CategoryList, just as you did in the GET method.
public ActionResult Create(ProjectVM model)
{
if (!ModelState.IsValid)
{
model.CategoryList = new SelectList(db.Categories, "ID", "Name"); // add this
return View(model);
}
// Save and redirect
}
To explain the inner workings (the source code can be seen here)
Each overload of DropDownList() and DropDownListFor() eventually calls the following method
private static MvcHtmlString SelectInternal(this HtmlHelper htmlHelper, ModelMetadata metadata,
string optionLabel, string name, IEnumerable<SelectListItem> selectList, bool allowMultiple,
IDictionary<string, object> htmlAttributes)
which checks if the selectList (the second parameter of #Html.DropDownListFor()) is null
// If we got a null selectList, try to use ViewData to get the list of items.
if (selectList == null)
{
selectList = htmlHelper.GetSelectData(name);
usedViewData = true;
}
which in turn calls
private static IEnumerable<SelectListItem> GetSelectData(this HtmlHelper htmlHelper, string name)
which evaluates the the first parameter of #Html.DropDownListFor() (in this case CategoryID)
....
o = htmlHelper.ViewData.Eval(name);
....
IEnumerable<SelectListItem> selectList = o as IEnumerable<SelectListItem>;
if (selectList == null)
{
throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture,
MvcResources.HtmlHelper_WrongSelectDataType,
name, o.GetType().FullName, "IEnumerable<SelectListItem>"));
}
Because property CategoryID is typeof int, it cannot be cast to IEnumerable<SelectListItem> and the exception is thrown (which is defined in the MvcResources.resx file as)
<data name="HtmlHelper_WrongSelectDataType" xml:space="preserve">
<value>The ViewData item that has the key '{0}' is of type '{1}' but must be of type '{2}'.</value>
</data>
according to stephens (user3559349) answer, this can be useful:
#Html.DropDownListFor(m => m.CategoryID, Model.CategoryList ?? new List<SelectListItem>(), "-Please select-")
or in ProjectVM:
public class ProjectVM
{
public ProjectVM()
{
CategoryList = new List<SelectListItem>();
}
...
}
Most Likely Caused some sort of error redirecting to your page and you not initializing your model's drop down lists again.
Make sure that you initialize your drop downs in either the model's constructor or every time before you send said model to the page.
Otherwise you will need to maintain the state of the drop down lists either through the view bag or through the hidden value helpers.
OK, the poster's canned answer neatly explained why the error occurred, but not how to get it to work. I'm not sure that's really an answer, but it did point me in the right direction.
I ran into the same issue and found a slick way to resolve it. I'll try to capture that here. Disclaimer - I work on web pages once a year or so and really don't know what I'm doing most of the time. This answer should in no way be considered an "expert" answer, but it does the job with little work...
Given that I have some data object (most likely a Data Transfer Object) that I want to use a drop-down list to supply valid values for a field, like so:
public class MyDataObject
{
public int id;
public string StrValue;
}
Then the ViewModel looks like this:
public class MyDataObjectVM
{
public int id;
public string StrValue;
public List<SectListItem> strValues;
}
The real problem here, as #Stephen so eloquently described above, is the select list isn't populated on the POST method in the controller. So your controller methods would look like this:
// GET
public ActionResult Create()
{
var dataObjectVM = GetNewMyDataObjectVM();
return View(dataObjectVM); // I use T4MVC, don't you?
}
private MyDataObjectVM GetNewMyDataObjectVM(MyDataObjectVM model = null)
{
return new MyDataObjectVM
{
int id = model?.Id ?? 0,
string StrValue = model?.StrValue ?? "",
var strValues = new List<SelectListItem>
{
new SelectListItem {Text = "Select", Value = ""},
new SelectListITem {Text = "Item1", Value = "Item1"},
new SelectListItem {Text = "Item2", Value = "Item2"}
};
};
}
// POST
public ActionResult Create(FormCollection formValues)
{
var dataObject = new MyDataObject();
try
{
UpdateModel(dataObject, formValues);
AddObjectToObjectStore(dataObject);
return RedirectToAction(Actions.Index);
}
catch (Exception ex)
{
// fill in the drop-down list for the view model
var dataObjectVM = GetNewMyDataObjectVM();
ModelState.AddModelError("", ex.Message);
return View(dataObjectVM);
)
}
There you have it. This is NOT working code, I copy/pasted and edited to make it simple, but you get the idea. If the data members in both the original data model and the derived view model have the same name, UpdateModel() does an awesome job of filling in just the right data for you from the FormCollection values.
I'm posting this here so I can find the answer when I inevitably run into this issue again -- hopefully it will help someone else out as well.
I had the same problem, I was getting an invalid ModelState when I tried to post the form. For me, this was caused by setting CategoryId to int, when I changed it to string the ModelState was valid and the Create method worked as expected.
In my case the first ID in my list was zero, once I changed the ID to start from 1, it worked.

MVC is it possible to target blank + parameters

See this :
#Html.ActionLink("link", "Func", new { controller = "MyControl" }, new { target = "_blank" })
It does what it is suposed to do, but what if i need my model, because here is the header of my function :
public ActionResult Func(model_1 m)
{ }
What i'm trying to do is open a new tab, and carry my model to this new tab... how can i do?
There is an overload of Html.ActionLink helper method which allows you to pass route values.
#Html.ActionLink("link", "Func", "MyControl" ,new { EmpId=1, EmpCode="E23" ,Age =23},
new { target = "_blank" })
This will basically generate an anchor tag with href value with querystring formed from the route values you provided.
link
Assuming you have a class with these 2 properties being used as the parameter of the action method
public class EmployeeVm
{
public int EmpId { set;get;}
public string EmpCode { set;get;}
public int Age{ set;get;}
}
and this is being used as the type of your action method argument
public ActoinResult Func(EmployeeVm model)
{
// To do : Return something
}
The model binder will be able to map the querystring values to the properties of the parameter object.
But remember, querystring's has limitations in how much data it can carry. Also the above approach work for a lean-flat view model class. It won't work for a complex viewmodel class where your properties are other classses /collection of other types.
In that case, The best solution is to pass a unique id / combination of Ids and use that to rebuild your model / view model in the second action method.
#Html.ActionLink("link", "Func", "MyControl" ,new { EmpId=1}, new { target = "_blank" })
and in your action method
public ActionResult Func(int empId)
{
// to do : Using empIdvalue, Get the View model /Model data
// Ex : EmployeeVm emp = SomeService.GetEmployeeFromId(empId)
}
Assuming SomeService.GetEmployeeFromId accepts an employeeId and return an object of EmployeeVm. The method can query your db table to get the corresponding employee record for the id passed in and build the EmployeeVm object from that data.

How to get ITemplate from razor engine (IRazorEngineService)

I just updated our RazorEngine reference to version 3.7.5. A bunch of things seems to have changed and became obsolete.
For most things I figured out 'the new way', except for 1 thing: getting an ITemplate instance.
We used to use a TemplateService instance. That had a method Resolve, which returns an ITemplate instance.
The TemplateService was replaced with IRazorEngineService. This doesn't have any method returning an ITemplate.
What's the correct way to retrieve one?
As I already discussed this on some threads here some quotes:
Can you elaborate the reasons why you need access to instances of that interface directly?
I decided to remove direct access to it as it isn't easy to use and mostly doesn't do what you think it does. It also can cause problems in case you use the Isolation API.
https://github.com/Antaris/RazorEngine/issues/225
If its about setting custom layouts the proper upgrade path is to use a custom TemplateBase and make use of the ViewBag (as discussed on the linked issue).
The other more interesting use case is to get data OUT of the template.
This is discussed in detail here: https://github.com/Antaris/RazorEngine/issues/238
Here is a code sample on how to get out the 'Subject' from the given template
Template:
#model HelloWorldModel
#{
Layout = "CI";
Subject = "Hello World";
}
Hello #Model.Name,<br/>
this is a test email...
Code (simplified)
class CustomDataHolder {
public string Destination { get; set; }
public string Subject { get; set; }
}
// In the custom TemplateBase class:
public string Subject { get { return Viewbag.DataHolder.Subject; }; set { Viewbag.DataHolder.Subject = value; } }
// Your code
public static Task SendEmailAsync<T>(string templateName, string destination, T model)
{
var holder = new CustomDataHolder ();
dynamic viewbag = new DynamicViewBag();
viewbag.DataHolder = holder;
holder.Destination= destination;
var body = Engine.Razor.Run(templateName, typeof(T), model, (DynamicViewBag)viewbag);
MailMessage msg = new MailMessage();
msg.To.Add(new MailAddress(holder.Destination));
msg.Subject = holder.Subject;
msg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(body, null, MediaTypeNames.Text.Html));
SmtpClient smtpClient = new SmtpClient();
return smtpClient.SendMailAsync(msg);
}
Hope this covers your use case. Otherwise please add more information to your question on what you trying to achieve with the ITemplate instances....

Add a PartialViewResult to a JsonResult

We are currently trying to create an action that returns a JsonResult and at certain times that action should also return some HTML inside it along with the other data. Is it possible to generate the HTML from another action that returns a PartialViewResult?
I think this is what you want to achieve.
Generate HTML from another action sounds strange.
You may get the same model from your repository and then render the same PartialView. You will need a method like the following.
// in a RenderingHelper class
public static string RenderViewToString(ControllerContext context, string viewName, object model)
{
if (string.IsNullOrEmpty(viewName))
viewName = context.RouteData.GetRequiredString("action");
ViewDataDictionary viewData = new ViewDataDictionary(model);
using (StringWriter sw = new StringWriter())
{
ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(context, viewName);
ViewContext viewContext = new ViewContext(context, viewResult.View, viewData, new TempDataDictionary(), sw);
viewResult.View.Render(viewContext, sw);
return sw.GetStringBuilder().ToString();
}
}
And then you will render the partial view accordingly:
string renderedHtml = RenderingHelper.RenderViewToString(this.ControllerContext, "~/Views/MyController/MyPartial", viewModel);
Where viewModel is the model used by "other action" too and "~/Views/MyController/MyPartial"is the partial view used by "other action" too.

ASP.NET MVC LINQ-2-SQL as model - how to update?

Is it possible to use LINQ2SQL as MVC model and bind? - Since L2S "attachement" problems are really showstopping.
[HttpPost]
public ActionResult Save(ItemCart edCart)
{
using (DataContext DB = new DataContext())
{
DB.Carts.Attach(edCart);
DB.Carts.Context.Refresh(RefreshMode.KeepChanges, edCart);
DB.Carts.Context.SubmitChanges();
DB.SubmitChanges();
}
return RedirectToAction("Index");
}
That does not work. :S
What does your Save View look like?
You can't just attach a new item to the EntitySet like that. -> Attaching requires a lot of checks and it is a real pain to implement. I tried it myself and didn't like it at all.
In your [HttpPost] method you'll need to update the model before you can save it:
[HttpPost]
public ActionResult Save(int id, ItemCart edCart) {
DataContext DB = new DataContext(); // I'm doing this without a using keyword for cleanliness
var originalCart = DB.Carts.SingleOrDefault(c => c.ID == id); // First you need to get the old database entry
if (ModelState.IsValid & TryUpdateModel(edCart, "Cart")) { // This is where the magic happens.
// Save New Instance
DB.SubmitChanges.
return RedirectToAction("Details", new { id = originalCart.ID });
} else {
// Invalid - redisplay with errors
return View(edCart);
}
}
It tries to update the model from the controllers valueprovider using they "Cart" prefix.