how to use linq for showing image in webgrid [duplicate] - asp.net-mvc

in web-grid i can not use navigation properties between my classes(products and productimages classes). for example i have used below code in web grid:
grid.Column("", "test",item=> (item.ProductImages.First().Id)+(item.Price))
but i got error:
'System.Collections.Generic.HashSet<WebStore.Models.ProductImage>' does not contain a definition for 'First'
my total code is like below:
#model IEnumerable<WebStore.Models.Product>
#using System.Linq;
#{
var grid = new WebGrid(source: Model, rowsPerPage: 5,ajaxUpdateContainerId:"divGrid");
}
#grid.GetHtml(tableStyle: "gridStyle", headerStyle: "gridHeader", rowStyle: "gridRow", alternatingRowStyle: null,htmlAttributes:new{Id="divGrid"},
columns: new WebGridColumn[] {
grid.Column("ProductName", "Product Name"),
grid.Column("Price", "Price"),
grid.Column("Description", "Description"),
grid.Column("CategoryName","Category Name",x=>x.Category.CategoryName),
grid.Column("", "test",item=> (item.ProductImages.First().Id)+(item.Price)),
grid.Column("","",x=>Html.ActionLink("Edit", "Edit", new{id=x.Id})),
grid.Column("","",x=>Html.ActionLink("Details", "Details", new{id=x.Id})),
grid.Column("","",x=>Html.ActionLink("Delete", "Delete", new{id=x.Id}))
}
)
this is my index view:
#model IEnumerable<WebStore.Models.Product>
#using System.Linq
#{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_LayoutCategory.cshtml";
}
<br/>
<p>
#Html.ActionLink("Create New", "Create")
</p>
<div id="divGrid">
#{ Html.RenderPartial("_ProductTitle", Model); }
</div>
this is my RenderPartial that use web grid before i posted:
#model IEnumerable<WebStore.Models.Product>
#using System.Linq
#ViewBag.test
#{
var grid = new WebGrid(source: Model, rowsPerPage: 5,ajaxUpdateContainerId:"divGrid");
}
#grid.GetHtml(tableStyle: "gridStyle", headerStyle: "gridHeader", rowStyle: "gridRow", alternatingRowStyle: null,htmlAttributes:new{Id="divGrid"},
columns: new WebGridColumn[] {
grid.Column("ProductName", "Product Name"),
grid.Column("Price", "Price"),
grid.Column("Description", "Description"),
grid.Column("CategoryName","Category Name",x=>x.Category.CategoryName),
grid.Column("", "test",item=>(int) (item.ProductImages.FirstOrDefault().Id)+(int)(item.Price)),
grid.Column("","",x=>Html.ActionLink("Edit", "Edit", new{id=x.Id})),
grid.Column("","",x=>Html.ActionLink("Details", "Details", new{id=x.Id})),
grid.Column("","",x=>Html.ActionLink("Delete", "Delete", new{id=x.Id}))
}
)

It's because definition of First is available in System.Linq. So, you should have System.Linq in your razor page :
#using System.Linq;
if you are using Linq in multiple page, you can add System.Linq namespace in web.config so that you do not need to write above using in each page. You can add namespace in web.config in following configuration :
<system.web.webPages.razor>
<pages>
<namespaces>
<add namespace="System.Linq" />
</namespaces>
</pages>
</system.web.webPages.razor>

i got the answer of my question with below code from the net:
I followed your steps and test your code and the same error display. So I think the format in your code can not be used in webgrid. So I think about another way to meet your requirement: we need a ViewModel to display what you want to show in the View and search the first item in Controller then save as ViewModel and pass the ViewModel to View, then we do not need search in the View. Here I will show you the steps with my demo.
Now, we have two model Product and Quantity that one product has many quantities. We should create a ViewModel.
ViewModel.cs:
public class ViewModel
{
public string Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public long quan { get; set; }
}
We can initialize some data and search the first value of Quantity depend on Product in Controller and assign to the ViewModel.
InventoryController .cs:
public class InventoryController : Controller
public ActionResult WebgridSample()
{
ObservableCollection<Product> inventoryList =
new ObservableCollection<Product>();
inventoryList.Add(new Product
{
Id = "P101",
Name = "Computer",
Description = "All type of computers",
quantity = new List<Quantity>
{
new Quantity {QUAN = 100 },
new Quantity {QUAN = 200 },
new Quantity {QUAN = 300 }
}
});
inventoryList.Add(new Product
{
Id = "P102",
Name = "Laptop",
Description = "All models of Laptops",
quantity = new List<Quantity>
{
new Quantity {QUAN = 400 },
new Quantity {QUAN = 500 },
new Quantity {QUAN = 600 }
},
});
inventoryList.Add(new Product
{
Id = "P103",
Name = "Camera",
Description = "Hd cameras",
quantity = new List<Quantity>
{
new Quantity {QUAN = 700 },
new Quantity {QUAN = 800 },
new Quantity {QUAN = 900 }
}
});
IEnumerable<string> model = (from sig in inventoryList
select new ViewModel
{
Name = sig.Name,
Description = sig.Description,
quan = sig.quantity.FirstOrDefault(),
}).ToList();
return View(model);
}
We can call each parameters of ViewModel in webgrid without using First().
WebgridSample.cshtml:
<div id="gridContent">
#grid.GetHtml(tableStyle: "webGrid",
headerStyle: "header",
alternatingRowStyle: "alt",
selectedRowStyle: "select",
columns: grid.Columns(
grid.Column("Id", ),
grid.Column("Name", " Name"),
grid.Column("Description", "Description", style: "description"),
grid.Column("Quantity", "quan "</i>)
))

Related

how to generate new row in webgrid on asp.net mvc4

I am having Text Box and a Button.I need to add Text Box value in Web grid when button was clicked.I coded to add text box value in grid but in the same column cell the value will be updated. I need to generate new column and add values ...
Index.cshtml Code
#{
#Html.TextBox("Value", "", new { id = "txtid" })
<input type="button" value="Submit" onclick="onSelectedIndexChanged()" id="btn" />
WebGrid grid = new WebGrid(Model, selectionFieldName: "SelectedRow");
#grid.GetHtml(
columns: grid.Columns(
grid.Column("Edit", header: null, format: #<text>#item.GetSelectLink("Edit")</text>),
grid.Column("Firstname", format: #<text>#item.GivenName</text>),
grid.Column("Surname", format: #<text>#item.Surname</text>),
grid.Column("Age", format: #<text>#item.Age</text>)
)
)
}
Models Code:
People.cs
public ObservableCollection<People> GetCustomerList(string firstname)
{
ObservableCollection<People> CustomerList = new ObservableCollection<People>();
DataTable dtCustomer = new DataTable();
CustomerList.Add(new People { Id = i, GivenName = firstname, Surname = "Kumar", Age = 25 });
i++;
return CustomerList;
}
Controller Code:
Home Controller.cs
public ActionResult GetPeople(string firstname)
{
//List<People> ItemList = new List<People>();
// ViewBag.Items = ItemList;
ObservableCollection<People> ItemList = new ObservableCollection<People>();
People Customer = new Models.People();
ItemList = Customer.GetCustomerList(firstname);
return PartialView("Index", ItemList);
}

Object reference not set to an instance of an object for dropdownlist in model

I have no idea why this is happening, I have set the values and debugged it, but it is just not passing the information from the controller to the view. Here is what is going on
Model:
public class QueueFilterModel
{
public string SelectedFilter { get; set; }
public string query { get; set; }
public List<string> MyFilterList { get; set; }
}
Controller:
[HttpGet]
public ActionResult Queue()
{
QueueFilterModel model = new QueueFilterModel()
{
SelectedFilter = "All",
query = "SELECT * FROM [CHAVI].[dbo].[TicketQueue]",
MyFilterList = new List<string>()
};
model.MyFilterList.Add("All");
model.MyFilterList.Add("Open");
model.MyFilterList.Add("Closed");
return View();
}
View:
#model RazorARPP.Models.QueueFilterModel
#{
ViewBag.Title = "Queue";
}
<h2>Queue</h2>
<form action="" method="post" enctype="multipart/form-data" id="MyForm">
Filter
<div>
Filter Options:
</div>
<div>
#Html.DropDownList("test", new SelectList(Model.MyFilterList,Model.SelectedFilter))
</div>
<h3>Insert Instructions Here</h3>
#{
var DB = Database.Open("CHAVI");
var grid = new WebGrid(DB.Query("SELECT * FROM [TicketQueue]"), null, null, 20);
#grid.GetHtml(
tableStyle: "webgrid",
columns: grid.Columns(
grid.Column(header: "Link", style: "labelcolumn", format: (item) => Html.ActionLink("Edit Item", "EditQueue", new { id = item.QueueID})),
grid.Column("Description", "Description"),
grid.Column("QueueDate", "QueueDate"),
grid.Column("Note", "Note"),
grid.Column("Status", "Status"),
grid.Column("LastUpdated", "LastUpdated")
)
)
}
</form>
The grid part is working fine (and the query). The problem is in the dropdown, it isn't set to anything there. Any thoughts? Thanks.
Are you not passing the model to view?
Should it not be
public ActionResult Queue()
{
QueueFilterModel model = new QueueFilterModel()
{
SelectedFilter = "All",
query = "SELECT * FROM [CHAVI].[dbo].[TicketQueue]",
MyFilterList = new List<string>()
};
model.MyFilterList.Add("All");
model.MyFilterList.Add("Open");
model.MyFilterList.Add("Closed");
return View(model);
}
Try using:-
public ActionResult Queue()
{
QueueFilterModel model = new QueueFilterModel()
{
SelectedFilter = "All",
query = "SELECT * FROM [CHAVI].[dbo].[TicketQueue]",
MyFilterList = new List<string>()
};
model.MyFilterList.Add("All");
model.MyFilterList.Add("Open");
model.MyFilterList.Add("Closed");
return View(model);
}

Posting DropDownList Value to MVC Controller

I'm trying to take information from a DropDownList and post the SelectListItem "Value" to another ActionResult method in the controller. The controller it will be passed to will take an integer value and use it in another query.
My controller method for populating the DropDownList is as follows:
public ActionResult SelectCategory()
{
var model = new TestTypesViewModel();
var query = (from ab in db.Tbl_Admin_Batch
from ub in db.Tbl_Admin_User_Batch
where ub.User_Id == 45875 && ab.Batch_Id == ub.Batch_Id
select ab).ToList();
model.Test_Types = query.Select(c => new SelectListItem
{
Text = c.Batch_Name,
Value = c.Batch_Id.ToString()
}).ToList();
return View(model);
My ViewModel for TestTypesViewModel is as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace HFI_Assessment_Administration.ViewModels
{
public class TestTypesViewModel
{
public int Batch_ID { get; set; }
public string Test_Type { get; set; }
public IEnumerable<SelectListItem> Test_Types { get; set; }
}
}
I'm new to MVC and trying to keep things simple, I know Batch_ID and Test_Type haven't been specified, but I'm not sure if they are even necessary at this point.
Any advice or help would be greatly appreciated, many thanks!
EDIT:
I now have a View for SelectCategory as follows:
#model HFI_Assessment_Administration.ViewModels.TestTypesViewModel
#{
ViewBag.Title = "SelectCategory";
}
#using (Html.BeginForm("Practice", "WebFormUserList"))
{
#Html.DropDownListFor(x => x.Batch_ID, Model.Test_Types)
<input type="submit" />
}
The controller it is being passed to is as follows:
[HttpPost]
public ActionResult Practice(TestTypesViewModel model, int Parent_ID = 45875)
{
var query = (from u in db.Users
join ur in db.User_Relationship on u.User_ID equals ur.Child_ID
join ub in db.Tbl_Admin_User_Batch on u.User_ID equals ub.User_Id
join ut in db.User_Tests on u.User_ID equals ut.User_ID into ps
from ut in ps.DefaultIfEmpty()
join lu in db.Lookups on u.First_LanguageID equals lu.LookupID
where ur.Parent_ID == Parent_ID && ub.Batch_Id == model.Batch_ID
group new { u, lu, ut } by new
{
u.User_ID,
u.Forename,
u.Surname,
u.Client_Code,
u.User_Name,
u.Password,
u.Email,
u.Gender,
u.Report_date,
u.EmailDate,
u.Job_Function,
lu.LookupValue
} into g
select new UserViewModel
{
User_ID = g.Key.User_ID,
Forename = g.Key.Forename,
Surname = g.Key.Surname,
Client_Code = g.Key.Client_Code,
User_Name = g.Key.User_Name,
Password = g.Key.Password,
Email = g.Key.Email,
Gender = g.Key.Gender,
Report_Date = g.Key.Report_date,
Email_Date = g.Key.EmailDate,
Test_Count = g.Count(p => p.ut.Test_ID != null),
Test_Completed = g.Count(p => p.ut.Completed != null),
Job_Function = g.Key.Job_Function,
Lookup_Value = g.Key.LookupValue
}).ToList();
return View(query);
}
The View for Practice is as follows:
#model IEnumerable<HFI_Assessment_Administration.ViewModels.UserViewModel>
#{
ViewBag.Title = "ChildUsers";
}
<h2>Practice</h2>
<div>
#{
var grid = new WebGrid(Model);
}
#grid.GetHtml(
tableStyle: "webgrid",
headerStyle: "webgrid-header",
footerStyle: "webgrid-footer",
alternatingRowStyle: "webgrid-alternating-row",
selectedRowStyle: "webgrid-selected-row",
rowStyle: "webgrid-row-style",
columns: grid.Columns
(
grid.Column(columnName:"User_ID", header: "User ID", style: "text-align-center"),
grid.Column(columnName:"Forename", header: "Forename", style: "text-align-center"),
grid.Column(columnName:"Surname", header: "Surname", style: "text-align-center"),
grid.Column(columnName:"Client_Code", header: "Client Code", style: "text-align-center"),
grid.Column(columnName:"User_Name", header: "User Name", style: "text-align-center"),
grid.Column(columnName:"Password", header: "Password", style: "text-align-center"),
grid.Column(columnName:"Email", header: "Email", style: "text-align-center"),
grid.Column(columnName:"Gender", header: "Gender", style: "text-align-center"),
grid.Column(columnName:"Report_Date", header: "Report Date", style: "text-align-center"),
grid.Column(columnName:"Email_Date", header: "Email Date", style: "text-align-center"),
grid.Column(columnName:"Test_Count", header: "Count", style: "text-align-center"),
grid.Column(columnName:"Test_Completed", header: "Completed", style: "text-align-center"),
grid.Column(columnName:"Job_Function", header: "Job Function", style: "text-align-center"),
grid.Column(columnName:"Lookup_Value", header: "Language", style: "text-align-center")
)
)
</div>
Everything is fine until I try to go to the next page of the grid or try to sort the grid. Upon where I get the error, Server Error in "/" Application. The resource cannot be found.
There are many ways to achieve that. You could either use a standard <form> tag or use AJAX to send the value.
Let's see the first case:
#model TestTypesViewModel
#using (Html.BeginForm("SomeAction", "SomeController"))
{
#Html.DropDownListFor(x => x.Test_Type, Model.Test_Types)
<button type="submit">OK</button>
}
and now in your target action:
[HttpPost]
public ActionResult SomeAction(TestTypesViewModel model)
{
// model.Test_Type will contain the selected value here
// Notice that if you intend to return the same view as the GET action
// (SelectCategory.cshtml) you should assign the Test_Types property on
// your model by querying your database the same way you did in the GET action
// before passing this model to the view. If on the other hand you intend to
// redirect here you don't need to assign it.
}
A second possibility is to use AJAX. So you could for example give your dropdownlist an id and have some link that when clicked it will invoke the target controller action sending it the selected value using AJAX:
#Html.DropDownListFor(x => x.Test_Type, Model.Test_Types, new { id = "testTypeDdl" })
#Html.ActionLink("click me", "SomeAction", null, new { id = "myLink" })
and then when some button or link is clicked use the $.ajax request:
$(function() {
$('#myLink').click(function() {
$.ajax({
url: this.href,
type: 'GET',
cache: false,
data: { selectedValue: $('#testTypeDdl').val() },
success: function(result) {
alert('The value was submitted to the server');
}
});
return false;
});
});
and now your controller action could have the following signature:
public ActionResult SomeAction(string selectedValue)
{
// Process the selected value here and return some result.
// This result could either be a PartialView or a JsonResult
// depending on your requirements.
}

How to show two Partials view data on Index.cshtml MVC3?

I have created a web application in mvc3 and created two partial views
one having controls two dropdownlist.
second having webgrid which shows data from database.
partialview1.cshtml
#model Mapping.Models.SecurityIdentifierMapping
#using (Html.BeginForm("Mapping", "Home"))
{
#Html.DropDownList("SecurityID", Model.PricingSecurityID, "-- Select SecurityID --")
<br />
#Html.DropDownList("CUSIPID", Model.PricingSecurityID, "-- Select CUSIPID --")
<br />
<button type="submit">Map</button>
}
partialview2.cshtml
#model IEnumerable<Mapping.Models.SecurityIdentifierMapping>
#{
ViewBag.Title = "Mapping";
WebGrid grid = null;
if (Model.Count() > 0 ){
grid = new WebGrid(source: Model,
defaultSort: "Id",
canPage: true,
canSort: true,
rowsPerPage:20);
}
}
<h2>Mapping</h2>
#if (grid != null)
{
#grid.GetHtml(
tableStyle: "grid",
headerStyle: "head",
alternatingRowStyle: "alt",
columns: grid.Columns(
grid.Column("", header: null, format: #<text>#Html.ActionLink("Edit", "Edit", new { id = (int)item.id }) #Html.ActionLink("Delete", "Delete", new { id = (int)item.id })</text>),
grid.Column("PricingSecurityID"),
grid.Column("CUSIP")
)
)
}
<br />
<p>
#Html.ActionLink("Back", "Index")
</p>
in index.cshtml
<div>
#Html.Partial("_ControlsPartial",)
</div>
<div>
#Html.Partial("_WebGridPartial")
</div>
inside Indexcontroller.cs in Index()
public ActionResult Index()
{
//FOR POPULATE DROPDOWN
//SecurityIdentifierMapping objModel = new SecurityIdentifierMapping();
//objModel.PricingSecurityID = objRepository.GetPricingSecurityID();
//objModel.CUSIP = objRepository.GetCUSIP();
//return View(objModel);
//FOR DISPLAY DATA IN WEBGRID
return View(dbContext.SecurityIdentifierMappings);
}
here problem is webgrid partial view is having#model IEnumerable<Mapping.Models.SecurityIdentifierMapping>
and
controlpartilview is having #model Mapping.Models.SecurityIdentifierMapping
so HOW CAN I CREATE A VIEWMODEL.cs A NEW CLASS WHICH WILL HAVE BOTH MODELS AND HOW CAN I WRITE IT IN INDEX(() SO THAT THAT METHOD WILL POPULATE DROPDOWN ALSO AND SHOW DATA IN WEBGRID ALSO ?
Why not just create a custom View Model class that contains two properties:
public class SecurityIdentifierMappingViewModel
{
public IEnumerable<SecurityIdentifierMapping> MappingList {get; set; }
public SecurityIdentifierMapping Mapping {get; set; }
}
Then you can pass this custom view model to the Index view and the corresponding property as the view model of each of the partials
EDIT
Your Index action would then look something like this:
public ActionResult Index()
{
// single mapping
var mapping = new SecurityIdentifierMapping();
maping.PricingSecurityID = objRepository.GetPricingSecurityID();
mapping.CUSIP = objRepository.GetCUSIP();
var viewModel = new SecurityIdentifierMappingViewModel
{
Mapping = mapping,
MappingList = dbContext.SecurityIdentifierMappings.ToList()
};
return View(viewModel);
}
And in Index.cshtml:
#model SecurityIdentifierMappingViewModel
<div>
#Html.Partial("_ControlsPartial", Model.Mapping)
</div>
<div>
#Html.Partial("_WebGridPartial", Model.MappingList)
</div>

Change value of grid item

I have table:
#using (Html.BeginForm()) {
#Html.ValidationSummary(true)
<fieldset>
<legend>Add</legend>
<br />
#{
var grid = new WebGrid(ViewBag.produkty,null, "names", 5);
}
#grid.GetHtml(
tableStyle: "grid",
headerStyle: "head",
alternatingRowStyle: "alt",
columns: grid.Columns(
grid.Column("name"),
grid.Column("value"),
grid.Column(header: "Add", format: (item) =>
new HtmlString(
Html.TextBoxFor(model => model.add).ToString())),
grid.Column( header: "Ok", format: (item) =>
new HtmlString(
Html.ActionLink("OK", "add_method", new { ID_name = item.ID_name }).ToString()))
)
)
</fieldset>
}
Controller:
public ActionResult use()
{
var nam = (from d in baza.Names
select new { d.ID_name, d.name, d.value}).ToList();
ViewBag.names= nam;
return View();
}
public ActionResult add_method(int ID_name, useModel use)
{
Use us = new Use();
var dat = DateTime.Today;
us.value = use.add;
us.ID_Name= ID_name;
us.data = dat;
baza.Zuzycies.InsertOnSubmit(us);
baza.SubmitChanges();
return RedirectToAction("use", "Product");
}
Model:
public class useModel
{
public int ID_name{ get; set; }
public decimal value{get;set;}
public string date { get; set; }
}
So, I have list of product on page. And I want to add a value (amount of product) into TextBox and press a ActionLink "OK" next to the textbox. How can I get amount of product in add_method? Or how insert submit button next to every one product (instead ActionLink "OK"), then is enought make use POST method...
You can use a grid componet with built-in edits functions (like the telerik Grid).
I think it's better to use ajax not reagular post request for your scenario.
Or you can do that ajax calls to the server with jquery, just send the parameters to the controller.

Resources