The name 'movieGenre' does not exist in the current context - asp.net-mvc-3

I write a ASP.NET MVC 3 project for service a video store. I add a CRUD MovieController class and add a search feature in it. But I receive an error: "The name 'movieGenre' does not exist in the current context" for the method. Here is the code:
public ActionResult SearchIndex(string searchString)
{
var GenreList = new List<string>();
var GenreQuery = from d in db.Movies
orderby d.Genre
select d.Genre;
GenreList.AddRange(GenreQuery.Distinct());
ViewBag.movieGenre = new SelectList(GenreList);
var movies = from m in db.Movies select m;
if (!String.IsNullOrEmpty(searchString))
{
movies = movies.Where(s => s.Title.Contains(searchString));
}
if (string.IsNullOrEmpty(ViewBag.movieGenre))
{
return View(movies);
}
else
{
return View(movies.Where(x => x.Genre == movieGenre));
}
return View(movies);
}
For the last movieGenre I'm receiving this error.

If you want do use a select list you have to use ViewData instead of a ViewBag.
ViewData["Genre"] = new SelectList(GenreList);

There is no movieGenre variable.
You mean ViewBag.movieGenre.

You have an error in method declaration.
You have:
public ActionResult SearchIndex(string searchString)
Should be:
public ActionResult SearchIndex(string movieGenre, string searchString)

Related

how to pass class parameter through Rotativa.ActionAsPdf

I want to pass class parameter in ActionAsPdf
public ActionResult Pdf(long Id)
{
var printclass = this._printService.GetPrintResults(Id);
return new ActionAsPdf("Content", new {Id = Id})
{
FileName = "abc.pdf"
}
}
public ActionResult Content(long Id)
{
//viewModel
return View("Index", viewModel);
}
It's working fine if Id alone is passed. But I want the printclass (var printclass of type class) to be passed in as the parameter as well to the Content.
I am having problem when I try to pass the class like below.
return new ActionAsPdf("Content", new {Id = Id, printclass= printclass})
{
FileName = "abc.pdf"
}
public ActionResult Content(long Id, printDTO abc)
{
var temp = abc;
//viewModel
return View("Index", viewModel);
}
The value of temp is null in the above case.
Use ViewAsPdf() instead. ActionAsPdf() accepts a RouteValueDictionary parameter.

retrieve data from database into web api

here is my table:
table name: data
fields:
Id
category
description
imagePath
I'm building a webApi that returns all imagePath or by id.
web api controller:
namespace task.Controllers
{
public class ImagesController : ApiController
{
DataEntry db = new DataEntry();
public IEnumerable<datum> GetImages()
{
var imagePath = from m in db.data select m;
return imagePath;
}
public IHttpActionResult GetImages(int id)
{
var imagePath = from m in db.data select m;
var imagPath = imagePath.Where((p) => p.Id == id);
if (imagePath == null)
{
return NotFound();
}
return Ok(imagePath);
}
}
}
It's returning all fields (Id,category, description and imagePath) instead of imagePath only.and for the select by Id method it's not working also, so what is wrong??
For the imagePath, try specifying it in the linq query like so:
public IEnumerable<string> GetImages()
{
var imagePath = from m in db.data select m.imagePath;
return imagePath.ToList();
}
For the select by id, try this:
public string GetImages(int id)
{
var imagePath = from m in db.data select m;
var imagPath = imagePath.Where((p) => p.Id == id);
if (imagPath == null)
{
return NotFound();
}
return Ok(imagPath.imagePath);
}
Double check for typo such as imagPath and imagePath too

Re-using code in controller class

The following code is taken from the tutorial: http://www.asp.net/mvc/tutorials/getting-started-with-aspnet-mvc3/cs/examining-the-edit-methods-and-edit-view which shows how ASP.net MVC 3 can be used to manage a movie database.
In the tutoral, a list object is added to the controller class that contains every movie genre that exists in the database. This list is then passed to a drop-down in the view enabling the database to be searched by genre.
Controller: (code related to movie genre in bold)
public ActionResult SearchIndex(string movieGenre, string searchString)
{
var GenreLst = new List<string>();
var GenreQry = from d in db.Movies
orderby d.Genre
select d.Genre;
GenreLst.AddRange(GenreQry.Distinct());
ViewBag.movieGenre = new SelectList(GenreLst);
var movies = from m in db.Movies
select m;
if (!String.IsNullOrEmpty(searchString))
{
movies = movies.Where(s => s.Title.Contains(searchString));
}
if (string.IsNullOrEmpty(movieGenre))
return View(movies);
else
{
return View(movies.Where(x => x.Genre == movieGenre));
}
}
What I want to do is enhance this further so that the movies can be searched by price as well as genre. I know I can re-use the much of the same code to do this. I think I need to create a new class that the controller class can pass either the genre or price. Is this correct? IF so, I'd appreciate an example. Thanks.
Update/Clarification:
I want to avoid repeating the code for both genre and price as below:
public ActionResult SearchIndex(string movieGenre, string searchString,float moviePrice)
{
var GenreLst = new List<string>();
var GenreQry = from d in db.Movies
orderby d.Genre
select d.Genre;
GenreLst.AddRange(GenreQry.Distinct());
ViewBag.movieGenre = new SelectList(GenreLst);
var PriceLst = new List<string>();
var PriceQry = from d in db.Movies
orderby d.Genre
select d.Genre;
PriceLst.AddRange(GenreQry.Distinct());
ViewBag.moviePrice = new SelectList(PriceLst);
var movies = from m in db.Movies
select m;
if (!String.IsNullOrEmpty(searchString))
{
movies = movies.Where(s => s.Title.Contains(searchString));
}
if (string.IsNullOrEmpty(movieGenre))
return View(movies);
else
{
return View(movies.Where(x => x.Genre == movieGenre));
}
if (string.IsNullOrEmpty(moviePrice))
return View(movies);
else
{
return View(movies.Where(x => x.Genre == moviePrice));
}
}
You just have to insert a text box in the view to get price value. Then receive this value at action and modify the query to get desired results.
like this:
#Html.ActionLink("Create New", "Create")
#using (Html.BeginForm()){
<p>Genre: #Html.DropDownList("movieGenre", "All")
Title: #Html.TextBox("SearchString")
Price: #Html.TextBox("Price")
<input type="submit" value="Filter" /></p>
}
And in the action method you are using the code below to populate the dropdownlist with genre values. You need not do the same for price value.
var GenreLst = new List<string>();
var GenreQry = from d in db.Movies
orderby d.Genre
select d.Genre;
GenreLst.AddRange(GenreQry.Distinct());
ViewBag.movieGenre = new SelectList(GenreLst);
And in your action method you just have to use the value of price to filter data
public ActionResult SearchIndex(string movieGenre, string searchString,float price)
{
var GenreLst = new List<string>();
var GenreQry = from d in db.Movies
orderby d.Genre
select d.Genre;
GenreLst.AddRange(GenreQry.Distinct());
ViewBag.movieGenre = new SelectList(GenreLst);
var movies = from m in db.Movies
select m;
if (!String.IsNullOrEmpty(searchString))
{
movies = movies.Where(s => s.Title.Contains(searchString));
}
if (string.IsNullOrEmpty(movieGenre))
return View(movies);
else
{
return View(movies.Where((x => x.Genre == movieGenre) &&(x => x.Price== price)));
}
}
You can do it in so many different ways while all are correct but it depends on the complexity of your project. Basically you don't want to over-engineer a simple program. But in general you should move all of your logic to a separate class and use your actions for creating and calling the right logic class:
public class GetMoviesRequest
{
public string Name { get; set; }
public float? Price { get; set; }
}
public class MoviesLogic
{
private List<Movie> Movies;
public IEnumerable<Movie> Get(GetMoviesRequest request)
{
IEnumerable<Movie> filtered = Movies.AsQueryable();
if (!string.IsNullOrEmpty(request.Name))
{
//Filter by name
filtered = filtered.Where(m => m.Name == request.Name);
}
if (request.Price.HasValue)
{
//Filter by value
filtered = filtered.Where(m => m.Price == request.Price);
}
return filtered;
}
}
public class MyController
{
public ActionResult SearchIndex(string movieGenre, string searchString)
{
var logic = new MoviesLogic();
var movies = logic.Get(new GetMoviesRequest() { Name = searchString } )
///do stuff with movies
}
}

How to get the userID of a logged in user?

I'm trying to get the userid of the currently logged in user. Here is my code:
public int GetUserID(string _UserName)
{
using (var context = new TourBlogEntities1())
{
var UserID = from s in context.UserInfoes
where s.UserName == _UserName
select s.UserID;
return Int32.Parse(UserID.ToString()); //error is showing here
}
}
I'm calling the method from my controller using this:
public ActionResult NewPost(NewPost model)
{
var Business = new Business();
var entity = new Post();
entity.PostTitle = model.PostTitle;
entity.PostStory = model.PostStory;
entity.UserID = Business.GetUserID(User.Identity.Name);
Business.NewPost(entity);
Business.ViewPost(entity);
return View("ViewPost", model);
}
The error is showing as "input string is not in correct format". Please help. Thanks.
Your query returns an IEnumerable. You need to get only the single record:
using (var context = new TourBlogEntities1())
{
var userIds = from s in context.UserInfoes
where s.UserName == _UserName
select s.UserID;
return userIds.Single();
}
By the way the .Single() method will throw an exception if there are more than 1 records matching the criteria. Hopefully you have an unique constraint on the Username field inside your database.
CreatedBy = this.HttpContext.User.Identity.Name,

How to edit row with Webgrid Razor MVC3

Can a WebGrid be made editable?
Make new View Edit.cshtml and take Hiddenfield for id that will pass to madel for the update data.
public ActionResult Edit(WebdridDBModel model, string id)
{
var editItem = from e in dc.EDetails where e.Id ==Convert.ToInt32(id) select e;
var editList = editItem.ToList();
model.FirstName= editList[0].FirstName;
model.LastName=editList[0].LastName;
model.Salary =Convert.ToInt32( editList[0].Salary);
return View(model);
}
public ActionResult EditSubmit(WebdridDBModel model, string id)
{
EDetail ed = dc.EDetails.Single(P=>P.Id==model.Id);
ed.FirstName = model.FirstName.Trim();
ed.LastName = model.LastName.Trim();
ed.Salary = model.Salary.ToString();
dc.SubmitChanges();
return RedirectToAction("Index");
}

Resources