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
Related
I'm implementing asp.net core 3.1. I'm passing a viewmodel to the razor view which is in the following:
public class BuyRequestViewModel
{
public IEnumerable<BuyRequest> BuyRequestVM { get; set; }
public IEnumerable<string> PlatesVM { get; set; }
}
My problem is, as my viewmodel is not of type Ienumerable, I'm getting error in foreach in the razor view. The error is like the following:
ForEach statement can not operate on variables of type 'MyPanel.ViewModels.BuyRequestViewModel' because 'MyPanel.ViewModels.BuyRequestViewModel' does not contain a public instance definition for 'GetEnumerator'
And my razor view code is like below:
#model MyPanel.ViewModels.BuyRequestViewModel
<table id="myDummyTable" class="table m-table mytable table-striped table-bordered">
<thead>
<tr>
<th>
Region
</th>
<th>
Zone
<th>
MyPlate
</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.BuyRequestVM.Select(x => x.Region))
</td>
<td>
#Html.DisplayFor(modelItem => item.BuyRequestVM.Select(x => x.Zone))
</td>
<td>
#Html.DisplayFor(modelItem => item.PlatesVM)
</td>
</tr>
}
</tbody>
</table>
And here below is the Index action in my controller:
public async Task<IActionResult> Index()
{
var bwrvm = new BuyRequestViewModel();
List<string> platesList = new List<string>();
var WasteAPIContext = _context.BuyRequest
.Include(b => b.UserWasteUnitNavigation).ToList();
bwrvm.BuyWasteRequestVM = WasteAPIContext;
var plateData = _context.Car.Select(x => x.Plate).ToList();
for (int i = 0; i < plateData.Count; i++)
{
//platesList.Add(plateData[i].ToString().Substring(2, 5));
string temp = getPlateCharacter(plateData[i].ToString().Substring(2, 3));
plateData[i].Remove(2, 3);
string totalPlate = plateData[i].Insert(2, temp);
platesList.Add(totalPlate);
}
bwrvm.PlatesVM = platesList;
return View(bwrvm);
}
I appreciate of any help.
#foreach (var item in Model)
should be
#foreach (var item in Model.BuyWasteRequestVM)
because Model == BuyRequestViewModel which contains 2 properties with lists.
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
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.
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)"/>
}
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)
}
}
}