I have created a public enum Interests
public enum Interests
{
Sport,
Party,
Gaming
}
And wanted to have a List of Enums in my user-Profile
public class User
{
public string Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public List<Interests> Interests = new List<Interests>;
}
I want to create a Checkbox-Form for my Enum-Values, which finally should get stored in my List, but can't find out how ...
I want to have for each Hobby in enum Interests a Checkbox-Field and every checked Checkbox should get stored in the List .
Try
checkboxID.DataSource = Enum.GetNames(typeof(Interests));
checkboxID.DataBind();
For MVC:
#Html.EnumDropDownListFor(m => m.Interests) in the view
Related
I am new to MVC, and learning as i go, but I am struggling to get to grips with DTO's with Web api.
I have 2 tables, one Schools, one students.
The School table has a one to many relationship with the Student table.
I can't seem to get the api response the way I want it.
This is the School DTO
public class SchoolDTO
{
public string schoolCode { get; set; }
public string schoolName{ get; set; }
public string studentNames { get; set; } // the related data
}
And this is what I am trying to do to populate it -
var schoolWithStudents = from b in context.Schools
select new SchoolDTO()
{
schoolCode = b.schoolCode,
schoolName= b.schoolName,
studentNames = b.Student.studentName
};
The response i am trying to get is something like this -
School
{schoolCode, schoolName}
StudentNames
[{…},{..}]
}
If you want to display student names which belong to a school, why studentNames property of SchoolDTO class is of type string ? It should be List<string>:
public class SchoolDTO
{
public string schoolCode { get; set; }
public string schoolName { get; set; }
public List<string> studentNames { get; set; }
}
And your Database Model should be something like that:
public class School
{
[Key] //I assume it is a PK
public string schoolCode { get; set; }
public string schoolName { get; set; }
public virtual ICollection<Student> Students { get; set; }
}
public class Student
{
[Key]
public Guid studentId { get; set; }
public string studentName { get; set; }
public string schoolCode { get; set; }
[ForeignKey("schoolCode")]
public virtual School School { get; set; }
}
So you can query the database like this:
var schoolWithStudents = context.Schools.Select(q => new SchoolDTO
{
schoolCode = q.schoolCode,
schoolName= q.schoolName,
studentNames = q.Students.Select(w => w.studentName).ToList()
})
.ToList();
Probably I'm doing this the wrong way, but this is a sample of the code I'm dealing with:
ViewModel
public class TasDataView
{
public int nTas { get; set; }
public string codTas { get; set; }
public decimal nValue { get; set; }
public bool nState { get; set; }
public System.DateTime nDate { get; set; }
public IEnumerable<Segments> ListSegments { get; set; }
public IEnumerable<Tas> ListTas { get; set; }
}
Segments class
public class Segments
{
public int segmentValue { get; set; }
public string segmentCode { get; set; }
public bool nState { get; set; }
public System.DateTime nDate { get; set; }
}
So, when I call the View with my controller, I send an TasDataView object as parameter:
public ActionResult AddTas()
{
TasDataView TDV = new TasDataView();
SegmentsManager SM = new SegmentsManager();
TDV.ListSegments = SM.DataSegments();
return View(TDV);
}
And here is the problem I'm facing. I need all the values of ViewModel for this View to work. So, for example, I have:
#model Project.Models.ViewModel.TasDataView
#Html.EditorFor(model => model.nValue, new { htmlAttributes = new { #class = "form-control" } })
And that will create the box for inserting whatever I want into nValue. But what if I want to access the ListSegments properties (from Segments class) that I sent to the View, so I can make a DropDown List ?
I can't do something like:
#Html.DropDownListFor(x => x.ListSegments.segmentValue, new SelectList(Model.ListSegments));
because all the ListSegments (like segmentValue) properties "are not in" TasDataView, but in Segments class.
How can I reorganize my DataView to be manageable for what I want? Instead of a List<> I should have plain variables?
I guess you're doing it wrong. The first argument of DropDownListFor is an expression that represents the property of the model that receives the value of the item selected in the drop down list. It must be some dedicate=d property somewhere on your model. e.g. selectedSegment.
EDIT:
So, if your goal is to present a list of values via model to the user in a drop down list and let the user pick a value, you'll need to:
have a property on the model to store the selected item (segment), e.g. selectedSegment,
properly render your segments as strings to present them.
The simplest solution is given below.
public class TasDataView
{
public int nTas { get; set; }
public string codTas { get; set; }
public decimal nValue { get; set; }
public bool nState { get; set; }
public System.DateTime nDate { get; set; }
public IEnumerable<Segments> ListSegments { get; set; }
public IEnumerable<Tas> ListTas { get; set; }
public Int32 selectedSegment;
}
#Html.DropDownListFor(x => x.selectedSegment, new SelectList(Model.ListSegments.Select(s => s.segmentCode)));
So there are no properties of the list you need to access.
I am developing a page which display data from service on the View and allow users to filter it.
There is a column for Countries, which allow users to filter.
I am not able to figure out a way to create a list of checkbox so that I can grab all selected values in one param like string[] countries (in the action method).
Can't use classic way :
<input type="checkbox" name="countries" value="USA" />USA<br />
<input type="checkbox" name="countries" value="Canada" />Canada<br />
This does pass the values in URL but doesn't set them back on post back (keeping checked onpostback).
I tried using checkboxlist (http://goo.gl/TUvZzu) but seemed to complicated for my Modal.
Since mine is a very straight forward Model :
public string Title { get; set; }
public string Website { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string Zip { get; set; }
public string State { get; set; }
public string Country { get; set; }
I appreciate your time and help.
You need to include a collection of countries within your view model to hold the selected values and allow them to be sent in the post.
I would also create a Country object to hold the Id, Name and Selected values.
In order for the model to be posted back you need to index each item in the view, this allows the model binder to pick it up.
Models
public class AModel
{
public AModel()
{
Countries = new List<Country>();
}
public string Title { get; set; }
public string Website { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string Zip { get; set; }
public string State { get; set; }
public string Country { get; set; }
// Country collection
public List<Country> Countries { get; set; }
}
public class Country
{
public int ID { get; set; }
public string Name { get; set; }
public bool Checked { get; set; }
}
View for loop
#for(var i = 0; i < Model.Countries.Count; i++)
{
<dt>
#Html.HiddenFor(m => Model.Countries[i].ID)
#Html.HiddenFor(m => Model.Countries[i].Name)
#Html.CheckBoxFor(m => Model.Countries[i].Checked)
</dt>
<dd>
#Model[i].Name
</dd>
}
Note the for loop instead of foreach to enable model binding and the hidden fields to allow the values to be posted back to the controller
http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/
Controller post
[HttpPost]
public ActionResult Index(AModel model)
{
//All the selected countries are available in the model
return View(model);
}
Working example
i want to join my list with comma to separate each itemlist from a class but it just can't giving me access to that class property
(i'm sorry i'm not good at explaning this, but i hope with this code sample you will understand)
public class CategoryDetail
{
public string id { get; set; }
public string title { get; set; }
public string href { get; set; }
public string type { get; set; }
public string icon { get; set; }
}
public class RootObjectDetail
{
public List<CategoryDetail> categories { get; set; }
public string categoryList
{
get
{
return string.Join<CategoryDetail>(",", categories.ToArray());
}
}
}
so this is my code that i use for retrieving data from json, and what i want is that in my xaml i will bind it into categoryList and it will showing categoryDetail title property that separated by comma
You don't need to specify generic parameter type - it can be inferred from usage. Also you need to project categories to sequence of titles:
public string categoryList
{
get
{
return String.Join(",", categories.Select(c => c.title));
}
}
I'm looking for a help.
I'm working an exercise based on MVC.
My model contains 2 classes. The DVD and the Category
public class DVD
{
public int ID
{ get; set; }
public string Title
{ get; set; }
public decimal Price
{ get; set; }
public int Quantity
{ get; set; }
public int Duration
{ get; set; }
public string Image
{ get; set; }
public virtual IList<Category> Categories_List
{ get; set; }}
Now the Category which is a method contains the following
public class Category
{
public int Id
{ get; set; }
public string Title
{ get; set; }
public virtual IList<DVD> DVDs
{ get; set; } }
Both DVD and Category are under the Model folder.
What I want to do is to create a list in my controller of the Category (inside the method of public ActionResult Details(int id = 0) )
to collect all the dvds which are in the category with the id 1 for example.
I found something simillar but I have problem when I have to check if the DVD's category id is the same with the one I'm looking for.
Search list of object based on another list of object c#
Can anyone help me to do it?
Thank you
Use LINQ Where. This will return call of the movies that match a particular category:
var moviesMatchingCategory = _masterMovieList.Where(ml => ml.Categories_List.Any(cl => cl.Id == categoryIdToCompare)).ToList();
If you can use linq this will help:
public IEnumerable<DVD> FindDvdByCategoryId(int categoryId, IEnumerable<DVD> dvdEnumerable)
{
return dvdEnumerable.Where(dvd => dvd.Categories_List.Any(c => c.Id == categoryId)).ToList();
}
List<DVD> list = dvds.Where(x=>x.Categories_List.Contains(y=>y.Id==Id)).ToList();
Try the above.