seems like I got a trouble when I am using MVCContrib.
I am using MVC4 to build a website and I want to show a datagrid that shows all the users
My data model is build by ADO.NET Entity Data Model, but I modify it so that I can use it easily in MVC4.
When I am trying to implement paging function, it throw an error: "The method 'Skip' is only supported for sorted input in LINQ to Entities. The method 'OrderBy' must be called before the method 'Skip'. "
source error is on: #Html.Grid(Model).Sort(ViewBag.SortModel as GridSortOptions).Columns(column =>
I have no idea how to fix this issue. in this case, if I am just using sorting, there is no problem. the issue only appears when I add paging function into the website (PS: I have tried only use paging, but it still giving me the same error).
Please help me :)
On the controller:
public ActionResult Index(int? page, GridSortOptions SortModel)
{
userRsy = new UserRepository();
if (CookieUserName == "")
{
return RedirectToAction("Index", "Home");
}
else
{
DBEntities _db = new DBEntities();
//ViewData.Model = _db.Users;
ViewBag.SortModel = SortModel;
IEnumerable<User> userList = _db.Users;
if (!string.IsNullOrEmpty(SortModel.Column))
{
userList = userList.OrderBy(SortModel.Column, SortModel.Direction);
//userList = userList.OrderBy(u => u.UserName);
}
//find login user
var _user = userRsy.Find(CookieUserName);
//ViewBag.Title = _user.FirstName + ", Welcom to E-Tabs Dashboard Management Center";
//Check user admin level
ViewBag.User = _user.FirstName;
ViewBag.Account = _user.UserName;
ViewBag.IsAdmin = false;
if (userRsy.AdminCheck(_user.UserName) == 99)
{ ViewBag.IsAdmin = true; }
//Pagination
userList = userList.AsPagination(page ??1,5);
//show view
return View(userList);
}
}
on the cshtml file:
#model IPagination<E_Tabs_Generic_Portal.Models.User>
#using MvcContrib.UI.Grid;
#using MvcContrib.Pagination;
#using MvcContrib.UI.Pager;
#using MvcContrib.Sorting;
#{
ViewBag.Title = "User";
Layout = "~/Views/Shared/_HomeLayout.cshtml";
}
<div class="content-1">
<h2>User List</h2>
#Html.Grid(Model).Sort(ViewBag.SortModel as GridSortOptions).Columns(column =>
{
column.For(p => p.UserName).Named("User Name");
column.For(p => p.IsEnabled).Named("Enabled");
column.For(p => p.FirstName).Named("First Name");
column.For(p => p.LastName).Named("Last Name");
column.For(p => p.Email).Named("E-Mail");
column.For(p => p.Company).Named("Company");
column.For(p => p.ServerName).Named("On Server #");
column.For(p => p.LastLoginTime).Named("Last Login").Format("{0:yyyy/MM/dd HH:mm}");
}
).Attributes( #class=>"Grid",#width =>"100%", #border =>"1", #style =>"text-align:center;border-collapse:collapse")
#Html.Pager(Model).First("First").Next(">>").Previous("<<").Last("Last").Format("Total{2}, Current{0}-{1} ")
</div>
I had similar problem when I was trying MVCCONTRIB grid first time last month. I fixed it by assigning Ipagination result to another variable and returning it as View.
something like this -
var temp = userList.AsPagination(page ?? 1, 5);
return View( temp);
Related
I have two tables tblProduct & tblImages. tblImages will have multiple images for a product and has a foreign key related to tblProduct. I have a problem with inserting data into multiple tables.
This is my view code:
<!-- Text Boxes and dropdown lists for tblProduct above and below is for adding files to tblImages-->
<div class="col-md-10">
<input multiple type="file" id="file" name="file" />
</div>
and here is my controller's code:
public ActionResult AddProduct_Post([Bind(Include = "productName, productDescription, productPrice, productCategory")]tblProduct tblProduct,List<HttpPostedFileBase> file)
{
List<tblImage> prodImages = new List<tblImage>();
var path = "";
foreach (var item in file)
{
if (item != null)
{
tblImage img = new tblImage();
img.ImageFile = new byte[item.ContentLength];
img.ImageName = string.Format(#"{0}.JPG", Guid.NewGuid());
img.vend_ID = Convert.ToInt32(Session["userID"]);
item.InputStream.Read(img.ImageFile, 0, item.ContentLength);
path = Path.Combine(Server.MapPath("~/Content/img"), img.ImageName);
item.SaveAs(path);
prodImages.Add(img);
}
}
tblProduct.venodrID = Convert.ToInt32(Session["userID"]);
tblProduct.tblImages = prodImages;
if (ModelState.IsValid)
{
db.tblProducts.Add(tblProduct);
db.SaveChanges();
int latestProdID = tblProduct.productID;
foreach (tblImage tblImg in tblProduct.tblImages)
{
tblImg.prod_ID = latestProdID;
db.Entry(tblImg).State = EntityState.Modified;
}
db.SaveChanges();
return RedirectToAction("DashBoard");
}
ViewBag.productCategory = new SelectList(db.tblCategories, "categoryID", "categoryName");
ViewBag.vendorGender = new SelectList(db.tblGenders, "genderId", "genderName");
return View();
}
I put a breakpoint on ModelState.IsValid and it is not going inside the if conditions and is returning back the same view without posting the data. Kindly tell me how to solve this issue
P.S: I am new to ASP.NET MVC
[Bind(Include =
you can the check parameter you bind in action method is similar what you defined in the View else you can remove the bind attribute and try this will help you to find what actually error is..
I am using u ComboBox component from Telerik (Kendo UI).
I followed all the steps mentioned on the Telerik site but somehow I am not able to filter correctly via the server. The filter works fine, everything gets passed except one issue. The filter only applys if I am clicking somewhere else so that the list hides from the combobox and if I re-click it then, the filter applys. So I have to toggle the list from the combobox and then I have to open the list again. This is very strange due I followed all the steps from Telerik.
I am filtering with my server which works correctly.
Code for my ComboBox:
#(Html.Kendo().ComboBox()
.Name("ModellDrop")
.Placeholder("Modell...")
.DataTextField("ModellKurz")
.DataValueField("ModellKurz")
.HtmlAttributes(new { ng_model = "Modell.ModellKurz" })
.Filter(FilterType.Contains)
.AutoBind(false)
.DataSource(source =>
{
source.Read(read =>
{
read.Action("GetModell", "UserApi")
.Data("filterModell");
})
.ServerFiltering(true);
})
.CascadeFrom("HerstellerDrop")
)
Code from my ServerSide:
[HttpGet]
public JsonResult GetModell(string Hersteller, string ModellFilter)
{
var List = _context.tbl_Hersteller_Modell.AsQueryable();
if(Hersteller != null)
{
List = List.Where(x => x.HerstKurz == Hersteller);
}
if (!string.IsNullOrEmpty(ModellFilter))
{
List = List.Where(p => p.ModellKurz.Contains(ModellFilter));
}
return Json(List, JsonRequestBehavior.AllowGet);
}
Code from my Client Side:
function filterModell() {
var Value = jQuery("#HerstellerDrop").data("kendoComboBox").value();
var FilterModell = jQuery("#ModellDrop").data("kendoComboBox").value();
return {
Hersteller: Value,
ModellFilter: FilterModell
};
}
Is something missing there or why is the list not automaticaly refreshing??
In my main INV_Assets controller of my MVC5 app, I have an method for Edit() which passes several populated SelectLists() via the ViewBag to allow users to select from all available entities for the relevant list in the other tables of my Database -- Note, if there is a better practice than passing this through the ViewBag, please feel free to guide me to a better way.
// GET: INV_Assets/Edit/5
public async Task<ActionResult> Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
INV_Assets iNV_Assets = await db.INV_Assets.FindAsync(id);
if (iNV_Assets == null)
{
return HttpNotFound();
}
ViewBag.Location_Id = new SelectList(db.INV_Locations, "Id", "location_dept", iNV_Assets.Location_Id);
ViewBag.Manufacturer_Id = new SelectList(db.INV_Manufacturers, "Id", "manufacturer_description", iNV_Assets.Manufacturer_Id);
ViewBag.Model_Id = new SelectList(db.INV_Models, "Id", "model_description", iNV_Assets.Model_Id);
ViewBag.Status_Id = new SelectList(db.INV_Statuses, "Id", "status_description", iNV_Assets.Status_Id);
ViewBag.Type_Id = new SelectList(db.INV_Types, "Id", "type_description", iNV_Assets.Type_Id);
ViewBag.Vendor_Id = new SelectList(db.INV_Vendors, "Id", "vendor_name", iNV_Assets.Vendor_Id);
return View(iNV_Assets);
}
My lists currently populate fine, but for ease of use I want to insert a value of "Add New" into each list, that when clicked will open pop-up (partial view?) of my relevant Create() View for the relevant entity. For example, if the Locations SelectList() has "Add New" clicked, I want to open my Create view for Locations.
Can anyone offer an example of how to do this?
I've been looking for how to insert a new value into the SelectList() but most of what I seem to be coming across is using the example of forgoing the SelectList() instead for an Html.DropDownList(), though I'm not sure why?
The SelectList class inherits IEnumerable<SelectListItem>, which are used to populate drop down lists.
Given a ViewModel object that has the following property:
public SelectList Options
{
get
{
var items = Enumerable.Range(0, 100).Select((value, index) => new { value, index });
SelectList s = new SelectList(items, "index", "value");
return s;
}
}
public int SelectedOption { get; set; }
The view:
#Html.DropDownListFor(m => m.SelectedOption, Model.Options, "Add New", new { #class = "form-control" })
In order to do what you want regarding the popup, you probably need some javascript to deal with this.
If you don't want the "Add New" in the DropDownListFor() you would need to add it manually to your collection before returning it to the view.
Hope this helps.
I want to do the menu page in a MVC project, there I need to populate some GridViews with different data from some models. First I created some datasets which contain the datatables; in the controller i call the index view which shows the main page.
public ActionResult Index()
{
..........
return View();
}
Index view
#{ Layout = "~/Views/Shared/_mainLayout.cshtml"; }
#{Html.RenderPartial("TicketsPartial");}
#Html.DevExpress().PageControl(s =>
{
s.Name = "tabcontrol";
s.TabPages.Add(tabPage =>
{
tabPage.Text = "Inbox";
tabPage.SetContent(() =>
{
Html.RenderPartial("InboxPartial");
});
});
s.TabPages.Add(tabPage =>
{
tabPage.Text = "Sent";
tabPage.SetContent(() =>
{
Html.RenderPartial("SentPartial");
});
});
}).GetHtml()
So I'm trying to use the Html.RenderPartial to call some partial views... here's the code in InboxPartial
#model DataModels.Email.DS_EmailMessages.DT_com_mail_messagesDataTable
#Html.DevExpress().GridView(s =>
{
s.Name = "Inbox";
s.KeyFieldName = "idticket";
s.Width = System.Web.UI.WebControls.Unit.Percentage(100);
}).Bind(Model).GetHtml()
So first I give the model to the view... the datatable from which the data should be loaded and I bind it to the gridview...
The thing is that this isn't displaying any data and also any column...and I really can't understand why... for example if I give the datatabel as a parameter in the controller like this:
public ActionResult Index()
{
..........
return View(dS_Tickets.DT_com_consultant_tickets);
}
And i erase the model from the views so there will not be any conflict, each partialview will show the columns and data taken from the datatabel, but only from that one table that I gave as a parameter.. I need to take the data from different tables... how can I do this?
Note: I'm using an extension called DevExpress v.12
in the Controller:
public ActionResult Create()
{
int i = 0;
string s = "";
bool unique = false;
while (!unique)
{
s = GenerateCode(i);
var CheckURLs = from x in db.QRCodeGs
where x.QRCodeShortString == s
select new { ShortCode = x.QRCodeShortString};
if (CheckURLs.Count() == 0)
{
unique = true;
}
else
{
i++;
}
}
return View(new QRCodeG { QRCodeShortString = s, QRCodeGenDate = DateTime.Today, LastEditDate = DateTime.Today, LastEditor = User.Identity.Name });
//return View();
}
Create.cshtml page:
<div class="editor-field">
#Html.EditorFor(model => model.QRCodeShortString)
#Html.ValidationMessageFor(model => model.QRCodeShortString) <br />(You make choose your own string or use this dynamically generated one)
</div>
Not sure exactly what the problem is, but here's a few things to check
Make sure the model is the proper type in the cshtml page. ie: #model QRCodeG
Make sure the variable 's' actually has something in it
Check your css (editor-field class) to make sure you aren't hiding it by mistake.
the first thing I would suggest is to move where you declare the model you are passing to the view, do something like
var qrCodeG = new QRCodeG { QRCodeShortString = s, QRCodeGenDate = DateTime.Today, LastEditDate = DateTime.Today, LastEditor = User.Identity.Name };
return qrCodeG;
then use the debugger to see if qrCodeG is being populated correctly.
if that works then try adding
<div> #model.QRCodeShortString </div>
to your view and see if that is outputting your data correctly
if that works look at what is going on in #Html.EditorFor(model => model.QRCodeShortString)