Something very simple but I am looking for the best way to do it. I have a Movie entity, each Movie can be in one Language only(a lookup table with English, French,etc...). Now I'm trying to load all the available languages in the lookup in the Movie Create Page, the Movie View Model:
namespace Project.ViewModels {
public class Movie {
[Key]
public int ID { get; set; }
public string Title { get; set; }
public string Rating { get; set; }
public string Director { get; set; }
public string Plot { get; set; }
public string Link { get; set; }
public string Starring { get; set; }
public int DateCreated { get; set; }
public string Genre { get; set; }
[Display(Name = "Language")]
public int LanguageID { get; set; }
// Navigational Properties
public virtual MovieLanguage Language { get; set; }
}
}
The MovieLanguage View model:
namespace MAKANI.ViewModels {
public class MovieLanguage {
[Key]
public int ID { get; set; }
public string Language { get; set; }
public virtual ICollection<Movie> Movies { get; set; }
}
}
The controller action:
public ActionResult MovieCreate() {
using (MAKANI.Models.Entities db = new MAKANI.Models.Entities()) {
List<Models.MoviesLanguages> enLanguages = db.MoviesLanguages.ToList();
IEnumerable<SelectListItem> selectList =
from m in enLanguages
select new SelectListItem {
Text = m.Language,
Value = m.ID.ToString()
};
ViewBag.SelectLanguage = selectList.ToList();
return View();
}
}
And in the View page i have
<div class="editor-field">
#Html.DropDownList("Language", ViewBag.SelectLanguage);
</div>
Howver I am getting this error in the View:
'System.Web.Mvc.HtmlHelper' has no applicable method named 'DropDownList' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax
Not sure what the problem might be?
Another questions regarding this approach:
Should a create a view model for the MovieLanguage entity in the first place, knowing that it servers only as a lookup table(so it doesnt require any Create/Edit/Delete action, Only List/Read maybe), should I be depending on the EF entities directly in that case?
Have a Languages Collection Property in your Movie ViewModel and a SelectedLanguage Property to get the selected Language ID when the form submits. It is not necessary that your ViewModel should have all the properties like your domain model. Have only those properties which the View needs.
public class Movie
{
public int ID { set;get;}
public string Title { set;get;}
//Other Relevant Properties also.
public IEnumerable<SelectListItem> Languages { set;get;}
public int SelectedLanguage { set;get;}
public Movie()
{
Languages =new List<SelectListItem>();
}
}
Now in your GET Action, Create an object of your Movie ViewModel and set the Languages Collection property and send that to the View. Try to avoid using ViewBag for passing data like this. ViewBag makes our code dirty.Why not use the strongly typed ViewModels to its full extent ?
public ActionResult CreateMovie()
{
var vm=new Movie();
// TO DO : I recommend you to abstract code to get the languages from DB
// to a different method so that your action methods will be
// skinny and that method can be called in different places.
var enLanguages = db.MoviesLanguages.ToList();
vm.Languages= = from m in enLanguages
select new SelectListItem {
Text = m.Language,
Value = m.ID.ToString()
};
return View(vm);
}
And in your view which is strongly typed to our Movie ViewModel, use the DropDownListFor Hemml helper method
#model Movie
#using(Html.Beginform())
{
#Html.DropDownListFor(x => x.SelectedLanguage,
new SelectList(Model.Languages, "Value", "Text"), "Select Language")
<input type="submit" />
}
Now when you post the form, you will get the selected languageId in the SelectedLanguage Property of your ViewModel
[HttpPost]
public ActionResult CreateMovie(Movie model)
{
If(ModelState.IsValid)
{
//check model.SelectedLanguage property here.
//Save and Redirect (PRG pattern)
}
//you need to reload the languages here again because HTTP is stateless.
return View(model);
}
Related
I need to create a POST form to add new objects to database. I have to create a Razor page where I can add new lesson form on click of a button. And after it on click of another button all the lessons should be added to DB context. I still don't know how to do it so I want you to help me
public class Course
{
public Guid Id { get; set; }
public string Category { get; set; }
public string Title{ get; set; }
public List<Lesson> Lessons { get; set; } = new List<Lesson>();
}
public class Lesson
{
public Guid Id { get; set; }
public string Title { get; set; }
public string Text { get; set; }
}
Here is some image of what I mean:
DB has a Course table and a Lesson table. Please tell me how I can create a page to create new 'Course' with dynamic amount of 'Lessons'
Please try with this in your controller method you need to pass from the page.
public ActionResult PostMethod(Course course, FormCollection formCollection)
{
string title = course.Title; // Title value
string category = course.Category; // Category value
//add course into database
if (course.Lessons != null && course.Lessons.Count > 0)
{
foreach (var item in course.Lessons)
{
//add Lessons into database
}
}
}
Please note in this example you are going to perform many operations in databases
you have to manage transaction as well for this.
Using Transactions or SaveChanges(false) and AcceptAllChanges()?
I have created a View Form which include SearchModel and result as well.
Here below is my search model.
public class UserViewModel
{
public string Status { get; set; }
public string Type { get; set; }
public string Search { get; set; }
public string SortBy { get; set; }
public string SortOrder { get; set; }
public IPagedList<Users> Users{ get; set; }
}
public ActionResult Index(UserViewModel filter, int? page)
{
filter.Users=GetUsersFromDatabase().ToList();
}
public ActionResult ToggleActive(bool IsActive, Guid Id)
{
// Set update operation to user
return RedirectToAction("Index", new { page = Request["page"] });
}
Now I want to redirect user on ToggleActive based on UserViewModel filter values on listing page. I hope you get my point.
How to pass the search only model. Please let me know the easy way.
In my view I have created the view.
<a href="#Url.Action("ToggleActive", "User", new { IsActive = item.IsActive, Id = item.UserID, page = ViewData["CurrentPage"]})">
Active/Deactive
</a>
Make sure your view has defined the model:
#model UserViewModel
Send your current model back to the controller's ToggleActive action:
public ActionResult ToggleActive(bool IsActive, Guid Id, int page, UserViewModel filter)
{
// Do stuff with the filter
// Set update operation to user
return RedirectToAction("Index", new { page = Request["page"] });
}
And change your action url to include the model:
<a href="#Url.Action("ToggleActive", "User", new { IsActive = item.IsActive, Id = item.UserID, page = ViewData["CurrentPage"], filter = Model})">
Active/Deactive
</a>
I have a two relational Model first one is
Teacher.cs
public class Teachers
{
[Key]
public int TeacherID { get; set; }
public string TeacherName { get; set; }
public string TeacherLname { get; set; }
public int DepartmentID { get; set; }
public string Image { get; set; }
public Department Department { get; set; }
}
and second is Department.cs
public int DepartmentID { get; set; }
public string DepartmentName { get; set; }
public string Image { get; set; }
public List<Teachers> Teachers { get; set; }
When I'm creating a new record, I' choose a Department Name for teacher, and It's adding fine. But When I want to Delete a record there is a error like this
The ViewData item that has the key 'DepartmentID' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'.
Line 32: #Html.DropDownList("DepartmentID", String.Empty)
I don't understand what I need to do. Can you help me?
Thanks a lot
TeacherController
EDIT :
//
// GET: /Teachers/Delete/5
[Authorize(Roles = "A")]
public ActionResult Delete(int id = 0)
{
Teachers teachers = db.Teachers.Find(id);
if (teachers == null)
{
return HttpNotFound();
}
return View(teachers);
}
//
// POST: /Teachers/Delete/5
[Authorize(Roles = "A")]
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Teachers teachers = db.Teachers.Find(id);
db.Teachers.Remove(teachers);
db.SaveChanges();
return RedirectToAction("Index");
}
When you pass an empty string into Html.DropDownList() it looks for a list of items to populate the dropdownlist from the first parameter in the ViewData collection. However, there is already an item in that collection that is of type Int32.
This is one of the many confusing scenarios that happen when you use Html.DropDownList() rather than using a strongly typed model and Html.DropDownListFor()
I suggest you do this:
#Html.DropDownListFor(x => x.DepartmentID, Model.Departments)
You will need to populate your model with a Departments object that is a list of Departments
I would like to try this
Telerik Grid throw a circular reference exception when I try to use an entityframework poco class into his binding. The code mentioned in the link propose to replace the json serializer used by Telerik with the NewtonSoft one. But Telerik Grid never call the create method from CustomGridActionResultFactory injected into the Grid. Does someone know the problem about this code (link above)?
There is a code library project which shows how to create a custom GridActionResultFactory. You may find it helpful.
You should use a "special" View Model for the TelerikGrid.
For examle if you use a Database Model something like this
public class Master {
public int Id { get; set; }
public string Name { get; set; }
public List<Detail> Details { get; set; }
}
public class Detail {
public int Id { get; set; }
public string Name { get; set; }
public Master Master { get; set; }
}
You need to create View Model as given below
public class MasterView {
public int Id { get; set; }
public string Name { get; set; }
}
public class DetailView {
public int Id { get; set; }
public string Name { get; set; }
public int MasterId { get; set; }
}
If you like me, think that creating a separate view models just for the telerik grid is overkill, you could feed your own Json to the grid created from a list of anonymous objects:
//This goes in the View:
Html.Telerik().Grid(Model)
.Name("Grid")
.Columns(columns =>
{
// your column mappings go here
})
.DataBinding(dataBinding => dataBinding.Ajax().Select("_yourMethodReturningJson", "YourControllerName", new { yourJsonMethodParameter = yourViewModel.someField }))
.Pageable()
.Sortable()
.Render();
And the Json method in your controller looks like this:
public JsonResult _yourMethodReturningJson(YourType? yourJsonMethodParameter)
{
var list = database.SomeCollection.Select(x => new
{
SomeColumnName = x.SomeField
});
return Json(list, JsonRequestBehavior.AllowGet);
}
Your could use a better Json library here if you like: http://james.newtonking.com/pages/json-net.aspx
In an Edit action in ASP.NET MVC, certain fields can be hidden from user with HiddenFieldFor. However this doesn't protect the fields (such as ID, data creation date) from being edited.
For example, a model Student has fields Id, Name and Birthday. I like to allow users to update the Name, but not Id nor Birthday.
For an Edit action like this
public ActionResult Edit(Student student)
{
if (ModelState.IsValid)
{
db.Entry(student).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(student);
}
How can I prevent Id and Birthday from being edited? Thanks!
You should use a view model which contains only the properties that you want to be edited:
public class EditStudentViewModel
{
public string Name { get; set; }
}
and then:
public ActionResult Edit(StudentViewModel student)
{
...
}
Another technique which I don't recommend is to exclude certain properties from binding:
public ActionResult Edit([Bind(Exclude = "Id,Birthday")]Student student)
{
...
}
or include:
public ActionResult Edit([Bind(Include = "Name")]Student student)
{
...
}
I assume you have to have the properties in your Model so that in View you can use them to render useful information e.g. an ActionLink with ID or some readonly Text.
In this case you can define your model with an explicit binding:
[Bind(Include = "Name")]
public class Student
{
int Id { get; set; }
int Name { get; set; }
DateTime Birthday { get; set; }
}
This way when updating your model, if the user submits an extra Id it will not be bound.
Another idea I like is having your model know its bindings per scenario and have them compiler validated:
public class ModelExpression<T>
{
public string GetExpressionText<TResult>(Expression<Func<T, TResult>> expression)
{
return ExpressionHelper.GetExpressionText(expression);
}
}
public class Student
{
public static string[] EditBinding = GetEditBinding().ToArray();
int Id { get; set; }
int Name { get; set; }
DateTime Birthday { get; set; }
static IEnumerable<string> GetEditBinding()
{
ModelExpression<Student> modelExpression = new ModelExpression<Student>();
yield return modelExpression.GetExpressionText(s => s.Name);
}
}
This way in your Action when calling TryUpdateModel you can pass this information.