How to get around when an image doesn't exist for a model? - asp.net-mvc-4

I have a problem to display the ImageNotFound when my database doesn't have an image set for an article in my webstore. I got the error Value cannot be null or empty.
The imageArticle is null when I debug it. I don't know but is seems the ImageArticle from the DB is causing this error. Any way I can't bypass it ?
I added an #if condition to check the null but it doesn't see to like it.
The database is containing a description, price and quantity but the image path has not been store. It is set to null. (this is because I want to allow the user to create new article in the webstore but maybe he doesn't have any picture yet for this new added item.
Here is the Browse.html
#model IEnumerable<MyWebStore.Models.Product>
#{
ViewBag.Title = "Browse";
}
<h2>Make your Selection</h2>
<table>
<tr>
<th>
#Html.DisplayNameFor(model => model.ImageArticle)
</th>
<th>
#Html.DisplayNameFor(model => model.Description)
</th>
<th>
#Html.DisplayNameFor(model => model.Price)
</th>
<th></th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#if (item.ImageArticle == null)
{
<img class="img_set_size" src="~/Images/ImageNotFound.jpg" />
} <==== the error occurs here
Else
{
<img class="img_set_size" alt="Image" src="#Url.Content(item.ImageArticle)"
/>
}
</td>
<td>
#Html.DisplayFor(modelItem => item.Description)
</td>
<td>
#Html.DisplayFor(modelItem => item.Price)
</td>
<td>
#Html.ActionLink("Add to Cart", "AddToCart", "Cart", new { id =
item.ProductId }, null)
</td>
</tr>
}
</table>

try like this:
#if (item.ImageArticle == null)
{
<img class="img_set_size" src="#Url.Content("~/Images/ImageNotFound.jpg")" />
}
else
{
<img class="img_set_size" alt="Image" src="#Url.Content(item.ImageArticle)"/>
}

Related

ASP.NET CORE LINQ GROUP BY

I have little problem with group by, i want achieve this
Nobukti : 001/des/2019
1000
2000
3000
4000
in My controller
var transaction = await (from a in _context.Transaction group a by a.Nobukti into pg
join b in _context.ChartAccount on pg.FirstOrDefault().Kodeakun
equals b.Kodeakun
select new Transaksi
{
Nobukti = pg.First().Nobukti,
Kodeakun = pg.FirstOrDefault().Kodeakun + b.Namaakun
}).ToListAsync();
In my View
<table class="table">
#foreach (var item in Model)
{
<thead>
<tr>
<th>
#Html.DisplayNameFor(model => model.Nobukti)
</th>
<td>
#Html.DisplayFor(modelItem => item.Nobukti)
</tr>
</thead>
<tbody>
<tr>
#foreach (var book in item.Nobukti)
{
<td>
#Html.DisplayFor(modelItem => item.Kodeakun)
</td>
}
</tr>
</tbody>
}
</table>
Can anyone help me? i dont know which wrong, my controller or my view,
sorry for english

ASP .NET Core: Editable Grid: Insert dropdown list

Objetive: I'm trying to construct a view where the records from a table (machines) can be updated in a grid, and that each atribute of each record has the option to be chosen from a dropdownlist.
This is my Get method:
public async Task<IActionResult> Test()
{
return View(await _context.Machines.Include(t => t.MachineTypes).Include(p => p.Suppliers).Include(s=>s.Stores).AsNoTracking().ToListAsync());
}
And this is the definition for the dropdown list:
private void PopulateMachineTypeDropDownListStore(object selectedStore = null)
{
var typequery = from k in _context.Stores
orderby k.StoreName
select k;
ViewBag.StoreID = new SelectList(typequery.AsNoTracking(), "StoreID", "StoreName", selectedStore);
}
Now, to the View:
Model:
#model IEnumerable<Application.Models.Machine>
Since I need all the registers. Then the table.
Header:
<table class="table">
<thead>
<tr>
<th>
#Html.DisplayNameFor(model => model.MchName)
</th>
<th>
#Html.DisplayNameFor(model => model.StoreID)
</th>
</tr>
</thead>
Body:
<tbody>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.MchName)
</td>
<td>
<div class="form-group">
<label asp-for="StoreID" class="col-md-2 control-label"></label>
<div class="col-md-10">
<select asp-for="StoreID" class="form-control" asp-items="ViewBag.StoreID">
<option value="">-- Seleccione Tienda --
</option>
</select>
<span asp-validation-for="StoreID" class="text-danger"></span>
</div>
</div>
</td>
</tr>
}
</tbody>
</table>
}
Question: I've read that is not best to use a div inside a td but it may work, altough there may be better options. Which would be the best practice?
Question: Well, this is a Test but I'm getting this error:
"'IEnumerable' does not contain a definition for 'StoreID' and no extension method 'StoreID' accepting a first argument of type 'IEnumerable' could be found (are you missing a using directive or an assembly reference?)"
I can't track the error. For me it looks fine and I don't know why it can't read the StoreID. I wonder if it's for the "asp-for" inside the div. I don't know.
Any help is appreciated.
Thanks

Update Partial view MVC4

I have this controller:
public ActionResult PopulateTreeViewModel()
{
MainModelPopulate mainModelPopulate = new MainModelPopulate();
// populate model
return View(mainModelPopulate);
}
That has a view like this:
#model xxx.xxx.MainModelPopulate
<table>
#foreach (var item2 in Model.CountryList)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item2.CountryName);
</td>
</tr>
foreach (var item3 in item2.BrandList)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item3.BrandName);
</td>
</tr>
foreach (var item4 in item3.ProductList)
{
<tr>
<td>
#Html.ActionLink(item4.ProductName, "FunctionX", new { idLab = item3.BrandID, idDep = item4.ProductID });
</td>
</tr>
}
}
}
</table>
The FunctionX controller is like this :
public ActionResult FunctionX(int idBrand=1 , int idProd=1)
{
List<ListTypeModel> typeModelList = new List<ListTypeModel>();
// populate typeModelList
return PartialView(typeModelList);
}
}
with this partial view:
#model IEnumerable<TControl.Models.ListTypeModel>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.Name)
</th>
<th></th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
</tr>
}
</table>
I want to add this partial view in my main view (PopulateTreeViewModel) and update the table with the relative type of product contained in Function X.
I tried also to substitute #Html.ActionLink with #Ajax.ActionLink and it performs the same way.
Have you tried #Html.RenderAction(item4.ProductName, "FunctionX", new { idLab = item3.BrandID, idDep = item4.ProductID });
There are other options too..! Pls refer http://www.dotnet-tricks.com/Tutorial/mvc/Q8V2130113-RenderPartial-vs-RenderAction-vs-Partial-vs-Action-in-MVC-Razor.html

Fetch id for viewpage

FIXED
what i want to do is a viewpage that contains accountslist by clicking on any of account name it should open selected accountid Payablerecords and Reciveablerecords. Note:Payables and Receivables are two Propertiest Taken From same DataModel tbl_Transaction(Which is collection).So can i get Id for collections?
I am getting the Selected AccountID records in Payable view but I cant get the same id related record in Reciveable view plz help me out.
Here is the code.
public class AccountsController : Controller
{
private AccountBs objBs;
public AccountsController()
{
objBs = new AccountBs();
}
// GET: Shinwari/Accounts
public ActionResult Index()
{
var accounts = objBs.GetALL();
return View(accounts);
}
<%=#model IEnumerable<BOL.tbl_Accounts>
#{
ViewBag.Title = "Index";
}
<h2>Accounts</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.Name)
</th>
<th>
#Html.DisplayNameFor(model => model.Contact)
</th>
<th>
#Html.DisplayNameFor(model => model.Discription)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.ActionLink(item.Name, "Index", "AccountDetailPayable", new {accountid=item.AId },null)
#*#Html.DisplayFor(modelItem => item.Name)*#
</td>
<td>
#Html.DisplayFor(modelItem => item.Contact)
</td>
<td>
#Html.DisplayFor(modelItem => item.Discription)
</td>
}
</table>%>
ReciveableAndPayablecontroller
private TransictionBs objbs;
public Details()
{
objbs = new TransictionBs();
}
// GET: Shinwari/AccountDetails
[HttpGet]
public ActionResult Index(int accountid)
{
ADetailsVm v = new ADetailsVm();
//Load both the collection properties
v.Payables = objbs.GetALL().Where(p => p.AId == accountid && p.tbl_TransictionType.Type.Contains("Payable")).ToList();
v.Reciveables = objbs.GetALL().Where(r => r.AId==accountid && r.tbl_TransictionType.Type.Contains("Reciveable")).ToList();
return View(v);
Veiw
#model BOL1.ADetailsVm
#{
ViewBag.Title = "Index";
}
<h2>AccountDetails</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table id="Payables" class="table">
<tr>
<th>
Date
</th>
<th>
Discription
</th>
<th>
Amount
</th>
</tr>
#foreach (var item in Model.Payables)
{
<tr>
<td>
#item.Date
</td>
<td>
#item.TDiscription
</td>
<td>
#item.Amount
</td>
</tr>
}
</table>
**Note Payable and reciveable both views are identical **
TO have same table work like 2 different properties you need to create new model class, In my case it is as following..
public ADetailsVm
{
List<tbl_Transiction>Payables{get;set;}
List<tbl_Transiction>Reciveables{get;set;}
}
And then add the following class to DBContext Class ... it would be like
public DbSet<ADetailsVm>ADetailsVm{get;set;}
You have to create new class where you need to add two lists as following.
public NewClass
{
List<tbl_Transiction>Payables{get;set;}
List<tbl_Transiction>Reciveables{get;set;}
}
and Add the class to your DbContext Class as follows.
public DbSet<NewClass>PayablesAndReceiveables{get;set;}
And loop your newly created lists objects in View where you need them.

how to retrieve data from dynamically created table in asp.net MVC 4

I am new to MVC 4 and I am stuck in a Problem, I have bound a table dynamically with some data from database as following on cshtml page
<table id="tblFeature">
<tr>
<th>
Include
</th>
<th>
Facilities
</th>
<th>
Description
</th>
</tr>
#foreach (var feature in Model)
{
<tr>
<td>#Html.EditorFor(item => feature.Checked)
</td>
<td>#Html.DisplayFor(item => feature.Name)
</td>
<td>#Html.TextAreaFor(item => feature.Description, 2, 70, null)
</td>
</tr>
}
</table>
Now I want to update the values like ,check or uncheck the checkbox,update the description in text area etc., in order to achieve that i need to add the updated values in the list in controller class and then I will update those values in database. but i don't know how to achieve that, it is something like following in plain english
foreach(feature in tblFeature)
{
if(feature is checked)
{
featureList.add(feature)
}
}
any help will be appreciated. thanks.
Change View for correct binding:
#using (Html.BeginForm("Save", "Controller"))
{
<table id="tblFeature">
<tr>
<th>
Include
</th>
<th>
Facilities
</th>
<th>
Description
</th>
</tr>
#for (int i = 0; i < Model.Count(); i++)
{
<tr>
#Html.HiddenFor(m => m[i].ProductSizeID)
<td>#Html.EditorFor(m => m[i].Checked)
</td>
...
</td>
</tr>
<input type="submit" value="save"/>
}
Post in Controller and save change
[HttpPost]
public ActionResult Save(IEnumerable<feature> ViewModels)
{
foreach (var feature in ViewModels)
{
if(feature.Checked)
{
featureList.add(feature)
}
}
}