MovieStoreEntities MovieDb = new MovieStoreEntities();
public ActionResult Edit(int id)
{
//var EditMovie1 = MovieDb
AddMovieModel EditMovie = (from M in MovieDb.Movies
from C in MovieDb.Categories
where M.CategoryId == C.Id
where M.Id == id
select new AddMovieModel { Name = M.Name, Director = M.Director, Country = M.Country, categorie = C.CategoryName, Category = M.CategoryId }).FirstOrDefault();
//AddMovieModel EditMovie1 = MovieDb.Movies.Where(m => m.Id == id).Select(m => new AddMovieModel {m.Id }).First();
List<CategoryModel> categories = MovieDb.Categories
.Select(category => new CategoryModel { Category = category.CategoryName, id = category.Id })
.ToList();
ViewBag.Category = new SelectList(categories, "Id", "Category");
return View(EditMovie);
}
//
// POST: /Default1/Edit/5
[HttpPost]
public ActionResult Edit(AddMovieModel Model2)
{
List<CategoryModel> categories = MovieDb.Categories
.Select(category => new CategoryModel { Category = category.CategoryName, id = category.Id })
.ToList();
ViewBag.Category = new SelectList(categories, "Id", "Category");
if (ModelState.IsValid)
{
//MovieStoreEntities model = new MovieStoreEntities();
MovieDb.SaveChanges();
return View("Thanks2", Model2);
}
else
return View();
}
This is the Code that I have wrote to edit Movie details and update database in the sql server.
This dont have any compile errors, But It didnt update sql server database.
Presuming here you are updating a category you would need to do something like
List<CategoryModel> categories = MovieDb.Categories
.Select(category => new CategoryModel { Category = category.CategoryName, id = category.Id })
.ToList();
ViewBag.Category = new SelectList(categories, "Id", "Category")
Category category = new Category()
category = categories.First(p=>p.CategoryId == Id);
category.Name = "New Name";
MovieDb.Categories.SaveChanges(category);
MovieDb.SaveChanges();
You will need to get the item you are wanting to edit...in this case a category which would be filtered from the list of categories. You can then call the savechanges method on that entity i.e. MovieDb.Categories.SaveChanges() and pass through the item that you want to update.
You need to use the Model2 object to create a new entity, add it to the ObjectContext and save the changes. You haven't written any code that should save anything to a database.
Related
I'm in a bit of a pickle rick and I need some help. Below is the code for my select lists in a form. I need the certification list to populate based off of what category they pick from the category list. How do I get this to work?
CODE:
// GET: INT_CertificationsXREF/Create
public IActionResult Create()
{
ViewBag.FullName = UserInformation.Globals.FullName;
ViewData["INT_CertificationCategoriesID"] = new SelectList(_context.INT_CertificationCategories, "ID", "Category");
ViewData["INT_CertificationConferredID"] = new SelectList(_context.INT_CertificationConferred, "ID", "ConferredBy");
ViewData["INT_CertificationsID"] = new SelectList(_context.INT_Certifications, "ID", "Certification").Where(i => i.CategoryID = ViewData["INT_CertificationCategoriesID"]);
ViewData["RIM_ResourceID"] = new SelectList(_context.RIM_Resource, "ID", "FirstName");
return View();
}
you can filter the list A with the list B using the function Contains
ViewData["INT_CertificationCategoriesID"] = new SelectList(_context.INT_CertificationCategories, "ID", "Category");
ViewData["INT_CertificationConferredID"] = new SelectList(_context.INT_CertificationConferred, "ID", "ConferredBy");
ViewData["INT_CertificationsID"] = new SelectList(_context.INT_Certifications, "ID", "Certification").Where(i => i.CategoryID.Contains(_context.INT_CertificationCategories.ID));
ViewData["RIM_ResourceID"] = new SelectList(_context.RIM_Resource, "ID", "FirstName");
I have the following ActionResult and retrieve records that starting with "query" parameter's value. However, when quesry value is empty or null, the methods returns no record while I want to retrieve all of them. So, do I have to use a if clause and create different lambda clauses, or is it possible to check query parameter and retrieve all of the records by using StartsWith?
public ActionResult StudentLookup(string query)
{
var students = repository.Students.Select(m => new StudentViewModel
{
Id = m.Id,
Name = m.Name
})
.Where(m => m.Name.StartsWith(query));
return Json(students, JsonRequestBehavior.AllowGet);
}
Well, two options:
Conditionally apply the Where clause:
IQuerable<StudentModel> students = repository.Students.Select(m => new StudentViewModel
{
Id = m.Id,
Name = m.Name
});
if (!string.IsNullOrEmpty(query))
{
students= students.Where(m => m.Name.StartsWith(query));
}
return Json(students, JsonRequestBehavior.AllowGet);
Put the check in the Where clause itself:
var students = repository.Students.Select(m => new StudentViewModel
{
Id = m.Id,
Name = m.Name
})
.Where(m => string.IsNullOrEmpty(query) || m.Name.StartsWith(query));
return Json(students, JsonRequestBehavior.AllowGet);
I'm using the Kendo UI for ASP.NET MVC and need to bind the selected value of a dropdown list to another dropdownlist. I want the user to only be shown the citiy entries that are applicable to the State selection they choose. My dropdownlist is as follows:
<div id="bindcity" class="inr_div" data-bind="visible: showFromChooseCity">
<h5 class="inr_hd">City<span>*</span>:</h5>
#(Html.Kendo().DropDownList()
.Name("fromCity")
.DataTextField("name")
.DataValueField("id")
.DataSource(source =>
{
source.Read(read =>
{
read.Action("GetCity", "Quote");
})
.ServerFiltering(true);
})
.SelectedIndex(0)
)
I setup several controller classes as follows:
public JsonResult GetZips()
{
FreightEntities freight = new FreightEntities();
var zips = freight.ZipCodes.AsQueryable();
var city = freight.ZipCodes.AsQueryable();
if (city != null)
{
zips = zips.Where(z => z.Zip = zips);
}
return Json(freight.ZipCodes.Select(z => new {Zip = z.Zip}), JsonRequestBehavior.AllowGet);
}
public JsonResult GetCity()
{
FreightEntities freight = new FreightEntities();
var state = freight.ZipCodes.AsQueryable();
var city = freight.ZipCodes.AsQueryable();
if (state != null)
{
city = city.Where(s => s.State = state);
}
return Json(freight.ZipCodes.Select(s => new { State = s.State }), JsonRequestBehavior.AllowGet);
}
public JsonResult GetState()
{
FreightEntities freight = new FreightEntities();
return Json(freight.ZipCodes.Select(s => new {State = s.State }), JsonRequestBehavior.AllowGet);
}
The above code now gives the error:
Cannot implicitly convert type 'System.Linq.IQueryable<FRCV2.Models.ZipCode>' to 'string'
Since I'm not using an ID field in my entities, what's the appropriate way of comparing the fields above?
How do I achieve this cascading dropdownlist effect?
I have four dropdownlistfor and a radiobuttonlistfor group. The radiobutton is for location type (which can either be an urban or rural location).
The first dropdownlistfor is the state which a user must select first before choosing either urban or rural. When the radiobuttonlistfor is selected it triggers a jquery click event which calls an action in my controller to populate the second dropdownlistfor for cities under the selected state and location type.
Controller
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult GetCitiesByStateId(int stateId)
{
if (stateId==0)
{
throw new ArgumentNullException("stateId");
}
var x = _repository.GetAllCitiesByStateId(stateId);
var result = (from s in x
select new
{
id = s.id,
name = s.CityName
}).ToList();
return Json(result, JsonRequestBehavior.AllowGet);
}
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult GetRuralArea(int stateId)
{
if (stateId == 0)
{
throw new ArgumentNullException("stateId");
}
var x = _repository.GetAllAreaInRural(stateId);
var result = (from s in x
select new
{
id = s.id,
name = s.AreaName
}).ToList();
return Json(result, JsonRequestBehavior.AllowGet);
}
View
State:
#Html.DropDownListFor(x => x.State.ID, Model._State(), "Select")
#Html.RadioButton("UrbanRural", "1", false, new { id = "urban" })
#Html.RadioButton("UrbanRural", "2", false, new { id = "rural" })
#Html.DropDownListFor(x => x.City.ID, Model._Cities)
#Html.DropDownListFor(x => x.AreaRural.ID, Model._AreaRural)
Button Click Code
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(Registration model)
{
if (ModelState.IsValid)
{
return RedirectToAction("Confirmation");
}
else
{
ViewBag.Selpwd = model.Password;
return View(model);
}
}
Issue:
I observed that when my button is clicked the city or area dropdownlistfor as the case may be empties. How do I retain the content after a postback?
How do I append default value to the my dropdownlistfor using the code below
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult GetCitiesByStateId(int stateId)
{
if (stateId==0)
{
throw new ArgumentNullException("stateId");
}
var x = _repository.GetAllCitiesByStateId(stateId);
var result = (from s in x
select new
{
id = s.id,
name = s.CityName
}).ToList();
return Json(result, JsonRequestBehavior.AllowGet);
}
My view code looks like this:
#Html.DropDownListFor(x => x.City.ID, Model._Cities)
#Html.ValidationMessageFor(x => x.City)
Thanks anticipation