mvc pop up window - asp.net-mvc

I have an mvc project with a jqgrid that has an editting column, my problem is when i Try to link the edit in each row to a pop up window, when I add the code below a window pop up opens but I recieve a 402 unothorized error, here is my code in the controller
var jsonData = new
{
total = totalPages,
page = page,
records = totalRecords,
rows = (
from item in items
select new
{
id = item.Id,
cell = new string[] {
"<A HREF=javascript: void(0) onclick=window.open
('/Views/viewname/Edit/'+"item.code+",'Edit','width=700,height=800'); >Edit</A>",
column2,
column3,
etc ...}
}).ToArray()
};
but if I just link the edit to a regular page through this code
var jsonData = new
{
total = totalPages,
page = page,
records = totalRecords,
rows = (
from item in items
select new
{
id = item.Id,
cell = new string[] {
"Edit " ,
column2,
column3,
etc ...}
}).ToArray()
I do not have any problems, I have a edit page which works fine if it is not a pop up window, has anybody faced this problem, or can anyone direct me to an example that is similar.Thnaks in advance

Have you tried with quotes around your href and onclick attribute values?

Related

Drop down list lose its value while post back in asp.net Mvc5 in partial view

This is a partial view. while submitting page is refreshed and my drop down value is gone.
#{
var pageName = ViewContext.RouteData.Values["action"].ToString();
ScanPaDataBaseApp.SCANHRConn dbConfig = new ScanPaDataBaseApp.SCANHRConn();
var MySalutList = dbConfig.Codes.ToList();
var SalutData = from Salut in dbConfig.Codes
where Salut.Typ.Trim() == "SALUT"
orderby Salut.SDes
select Salut;
SelectList GetSalutList = new SelectList(SalutData, "Cd", "sDes");
ViewBag.SalutList = GetSalutList;
#Html.DropDownList("Salutation", ViewBag.SalutList as SelectList, "Select Salut", new { Style = "width:236px; color:#858585;" })
Use #Html.DropdownListFor which automatically bind your list to model and you can use it. :)

ASP.NET MVC 5, Storing list values in Session

I am trying to store list of products in List<> which I have stored in session, but when I add second product it only shows first in my View page.
..............
List<ShoppingCartItem> ShoppingCartItems = new List<ShoppingCartItem>
{
new ShoppingCartItem() {Product = product.Name, Attributes = atts, Options = opts, Price = producttotalprice, Quantity = 1}
};
if (Session["Cart"] == null)
{
Session["Cart"] = ShoppingCartItems;
}
return View(Session["Cart"]);
}
Anybody can help me please to retrieve all products I have stored.
You're creating a new List<ShoppingCartItem> every time and only ever putting one element in that list. It sounds like you want to first check if there's already a list in the session. And, if so, add the new element to that list. Something like this:
List<ShoppingCartItem> shoppingCartItems;
if (Session["Cart"] != null)
{
shoppingCartItems = (List<ShoppingCartItem>)Session["Cart"];
}
else
{
shoppingCartItems = new List<ShoppingCartItem>();
}
shoppingCartItems.Add(new ShoppingCartItem() {Product = product.Name, Attributes = atts, Options = opts, Price = producttotalprice, Quantity = 1});
Session["Cart"] = shoppingCartItems;
return View(shoppingCartItems);
If that is the "add" code, you're not actually amending the list. You're declaring an entirely new ShoppingCartItems list which contains the new product.
It works for the first product because this returns true:
if (Session["Cart"] == null)
The second time around this is false, and nothing happens. What you want to do is:
1) Retrieve the session cart which is a 'List' (if it is null, then initialise a new one.
2) Create a new ShoppingCartItem from the product which has been passed to the controller. Add that product to the cart.

Issue With Showing Distinct fields using Linq

var getAllProducts = _productService.GetAllProducts();
if (productstest.Count > 0)
{
model.idproduct.Add(new SelectListItem()
{
Value = "0",
Text = _localizationService.GetResource("Common.All")
});
foreach (var m in getAllProducts)
model.idproduct.Add(new SelectListItem()
{
Value = m.Id.ToString(),
**Text = m.Size.Distinct().ToString(),**
Selected = model.Pid == m.Id
});
}
public virtual IList<Product> GetAllProducts(bool showHidden = false)
{
var query = from p in _productRepository.Table
orderby p.Name
where (showHidden || p.Published) &&
!p.Deleted
select p;
var products = query.ToList();
return products;
}
The issue is even i tried to populate the select list with distinct size using: Text = m.Size.Distinct().ToString(), but it shows the duplicate for instance 100 products are of size 33 cm , the list will populate the dropdownlist in the view with 33cm occuring 100 times , I dont want to show 100 times , just want to show 1 time, Can any one assist me with this issue ?
Presumably you are only trying to show one product of each different size... if so initialising your getAllProducts variable like so will do the trick:
var getAllProducts = _productService.GetAllProducts().GroupBy(p => p.Size).Select(g => g.First());

Problem with Json

Im having this weird problem with creating Json dynamically.... for some reason this doesnt work
var jsonData = new
{
total = totalPages,
page = page,
records = totalRecords,
rows = (
from company in companies
select new
{
i = company.Id,
cell = new string[] { company.Id.ToString(), ""+company.Name.ToString()+"" }
}).ToArray()
};
Its giving me a weird "Could not translate expression....... into sql" exception
but with this minor change it works just fine
var jsonData = new
{
total = totalPages,
page = page,
records = totalRecords,
rows = (
from company in companies
select new
{
i = company.Id,
cell = new string[] { company.Id.ToString(), ""+company.Name.ToString()+"" }
}).ToArray()
};
Notice that the change is just to make id=5 instead of dynamic.
also, this works correctly but I dont like it.
var jsonData = new
{
total = totalPages,
page = page,
records = totalRecords,
rows = (
from company in companies
select new
{
i = company.Id,
cell = new string[] { company.Id.ToString(), ""+company.Name.ToString()+"" }
}).ToArray()
};
I'm not sure if this will solve your problem, but assuming companies is an IQueryable from the DataContext, try calling ToList() on it so that the select expression doesn't get sent to the database.
var jsonData = new
{
total = totalPages,
page = page,
records = totalRecords,
rows = (
from company in companies.ToList()
select new
{
i = company.Id,
cell = new string[] { company.Id.ToString(), ""+company.Name.ToString()+"" }
}).ToArray()
};
It's trying to translate the i to SQL and failing. The query that gets generated is something close to
SELECT companies.Id,
'' + companies.Name + ''
FROM companies
Basically, your string concatenation is being sent to your SQL server.

Selected Item doesnt get selected

I have a problem with a selectlist, I have 8 items and 3 of them gets the value selected = true in debug, but the rendered Html the item isnt selected.
What might be wrong?
List<SelectListItem> UsergroupID = (from usg in _ug.GetUsergroups().ToList()
join ug in u.Usergroups
on usg.UsergroupID equals ug.UsergroupID into j
select
new SelectListItem
{
Selected = j.Any(),
Value = usg.UsergroupID.ToString(),
Text = usg.UsergroupName
}).ToList();
ViewData["UsergroupID"] = UsergroupID;
return View("UserEdit", new UserAdminEditViewModel { User = u, Usergroups = _ug.GetUsergroups() });
And in my view I have:
<%= Html.ListBox("UsergroupID", (IEnumerable<SelectListItem>)ViewData["UsergroupID"]) %>
What's the reason for it not making the 3 items that have selected = true selected in the selectlist?
/M
looks like another bug in MVC to me ... you can refer to this link explaining part of the issue Link

Resources