How to reference images from a diferent table C# - c#

I have two tables, Libro (books) and Imagenes (Images) where Id on Libro is referenced on Imagenes as a fk on IdLibro so that I have an image for each Libro.
I want to reference the image on a Bootstrap card but they are on different tables on the database, how do I do it?
Here is what I have
#{
foreach (var lib in Model)
{
<div class="card col-md-3 m-2" style="width: 18rem;">
<img src="#Html.DisplayFor(modelItem => lib.Imagenes)" class="card-img-top" alt="There should be an image here">
<div class="card-body">
<h5 class="card-title">#lib.Titulo</h5>
#switch (lib.Saga)
{
case 0:
<h6> </h6>
break;
case 1:
<h6>Canción de Hielo y Fuego</h6>
break;
case 2:
<h6>Harry Potter</h6>
break;
}
<br />
<div class="row m-2">
<div class="col-md-12">
<p class="card-text">$#lib.Precio</p>
</div>
</div>
<br />
<div class="row m-2 position-absolute bottom-0">
<div class="col-md-4">
<a class="btn btn-outline-primary" href="/Home/Detalle/#lib.Id">Ver</a>
</div>
<div class="col-md-1"></div>
<div class="col-md-4">
<a onclick="Agregar(#lib.Id)" class="btn btn-outline-primary" id="#lib.Id">Agregar</a>
</div>
</div>
</div>
</div>
}
}

If the structure of your classes is as follows, you can easily access the Imagenes class values
public class Libro
{
public int Id { get; set; }
//........................
public ICollection<Imagenes> Imagenes { get; set; }
}
public class Imagenes
{
public int ID { get; set; }
//..............................
public int IdLibro { get; set; }
public Libro Libro { get; set; }
}
in view
#{
foreach (var lib in Model)
{
<img src="Url.Content(lib.Imagenes.FirstOrDefault().ImageUrl)" />
}
}

Related

When i try to get data from data for edit page it's showing me error

When I try to get data from data for edit page it's showing me the following error:
So I am building a website where I want to implement a edit banner form when the User can Edit his banner data.
And here is where the problem I raise, when I run the code, it return always as null.
Here is my controller:
[HttpGet]
public ActionResult EditBanner(int id)
{
var abd = (from a in _db.Banner
where id == a.BannerId
select a).FirstOrDefault();
return View(abd);
}
[HttpPost]
public ActionResult UpdateBannerDatas(BannerViewModels bnrupdate)
{
var bnrdata = _db.Banner.Where(x => x.BannerId == bnrupdate.BannerId).FirstOrDefault();
if (bnrdata != null)
{
bnrdata.BannerTitle = bnrupdate.BannerTitle;
bnrdata.BannerUrl = bnrupdate.BannerUrl;
bnrdata.BannerIndex = bnrupdate.BannerIndex;
bnrdata.BannerDescription = bnrupdate.BannerDescription;
bnrdata.BannerIndex = bnrupdate.BannerIndex;
_db.SaveChanges();
}
return Redirect("BannerDetails");
}
Here is my viewmodel:
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SZGMC.Web.Models;
namespace SZGMC.Web.Areas.Admin.ViewModels
{
public class BannerViewModels
{
public int BannerId { get; set; }
public string BannerTitle { get; set; }
public string BannerDescription { get; set; }
public string BannerImg { get; set; }
public IFormFile BannerImg1 { get; set; }
public string BannerUrl { get; set; }
public string BannerIndex { get; set; }
public int? BMasterId { get; set; }
public byte? IsDeleted { get; set; }
public virtual BannerMaster BMaster { get; set; }
}
}
Here is my model:
using System;
using System.Collections.Generic;
// Code scaffolded by EF Core assumes nullable reference types (NRTs) are not used or disabled.
// If you have enabled NRTs for your project, then un-comment the following line:
// #nullable disable
namespace SZGMC.Web.Models
{
public partial class Banner
{
public int BannerId { get; set; }
public string BannerTitle { get; set; }
public string BannerDescription { get; set; }
public string BannerImg { get; set; }
public string BannerUrl { get; set; }
public string BannerIndex { get; set; }
public int? BMasterId { get; set; }
public byte? IsDeleted { get; set; }
public virtual BannerMaster BMaster { get; set; }
}
}
Here is my view:
<form asp-controller="Home" asp-action="UpdateBannerDatas" enctype="multipart/form-data" method="post">
<div id="main-content">
<div class="container-fluid">
<!-- Page header section -->
<div class="block-header">
<div class="row clearfix">
<div class="col-lg-6 col-md-5 col-sm-12">
<h1>Hi, Welcomeback!</h1>
<span>You can edit banner here</span>
</div>
<div class="col-xl-6 col-md-7 col-sm-12 text-md-right">
<div class="d-flex align-items-center justify-content-md-end mt-4 mt-md-0 flex-wrap vivify pullUp delay-550">
<div class="mb-3 mb-xl-0 ">
<a asp-action="BannerDetails" class="btn btn-dark">Banner List</a>
</div>
</div>
</div>
</div>
</div>
<div class="row clearfix">
<div class="col-12">
<div class="card">
<div class="body">
<div class="header">
<h2><strong>Enter Banner Details</strong></h2>
</div>
<br />
<input asp-for="BannerId" type="hidden" />
<div class="row">
<div class="col-12">
<div class="form-group c_form_group">
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text"></span>
</div>
<input type="text" class="form-control" asp-for="BannerTitle" placeholder="Banner Title" aria-label="bannertitle" aria-describedby="basic-addon1">
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-6">
<div class="form-group c_form_group">
<label>Banner Description</label>
<div class="input-group">
<textarea class="form-control" asp-for="BannerDescription" aria-label="Banner Description" rows="6"></textarea>
</div>
</div>
</div>
<div class="col-6">
<div class="drop-zone">
<span class="drop-zone__prompt">Drop file here or click to upload</span>
<input type="file" asp-for="BannerImg1" name="myFile" class="drop-zone__input" accept="image/*" data-allowed-file-extensions='["jpg", "png" , "jpeg"]' required>
</div>
</div>
</div>
<div class="row">
<div class="col-6">
<div class="form-group c_form_group">
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text"></span>
</div>
<input type="text" class="form-control" asp-for="BannerIndex" placeholder="Banner Index" aria-label="bannerindex" aria-describedby="basic-addon1">
</div>
</div>
</div>
<div class="col-6">
<div class="form-group c_form_group">
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text"></span>
</div>
</div>
</div>
</div>
</div>
<div class="mb-2" align="center">
<button type="submit" class="btn btn-success btn-round">Edit Banner</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</form>
When i try To Edit the data in the browser it Throw exception, in database i have value but when i tried to fetch data its showing error.
thank you in advance to everybody for any help or advice.
According to the error message it is necessary to pass the BannerViewModels instance instead of the Banner entity:
public ActionResult EditBanner(int id)
{
var abd = _db.Banner.Where(a => a.BannerId == id)
.Select(b => new BannerViewModels()
{
BannerId = b.BannerId,
BannerTitle = b.BannerTitle,
BannerDescription = b.BannerDescription,
BannerImg = b.BannerImg,
BannerUrl = b.BannerUrl,
BannerIndex = b.BannerIndex,
BMasterId = b.BMasterId,
IsDeleted = b.IsDeleted
})
.FirstOrDefault();
return View(abd);
}
Passing an entity to the view as a data model is bad idea.
See the following post: Why it's not a good idea to pass entities as Models in MVC?

multiple image list failed to be displayed

I want to display my multiple image list which I uploaded my specific product. but when I using a loop for display, I don't understand why?.here is my code:
Model
//model for multiple images
public class Photo1
{
[Key]
public int PhotoId { get; set; }
public string Image { get; set; }
}
//main model
public class Shop
{
public int Id { get; set; }
[Required]
[Display(Name = "product name")]
public String Name { get; set; }
[Required]
public int Price { get; set; }
public String Image { get; set; }
public String Image1 { get; set; }
public List<Photo1> Photos { get; set; }
[Required]
public int Quantity { get; set; }
[Required]
public bool IsAvailable { get; set; }
[Display(Name = "Category")]
public int? CategoryTypeId { get; set; }
[ForeignKey("CategoryTypeId")]
public Category Category { get; set; }
[Display(Name = "SubCategory")]
public int? SubCategoryTypeId { get; set; }
[ForeignKey("SubCategoryTypeId")]
public SubCategory SubCategory { get; set; }
}
Controller
[HttpGet]
public ActionResult Details(int? id)
{
if (id == null)
{
return NotFound();
}
var product = _db.Shop.Include(c => c.Category).Include(c => c.SubCategory).FirstOrDefault(c => c.Id == id);
if (product == null)
{
return NotFound();
}
return View(product);
}
}
Details.cshtml
#model DigitalShop.Models.Shop
#{
ViewData["Title"] = "Details";
}
<br />
<h2 class="text-danger"> Product Details</h2>
</br></br>
#*<div class="col-8 position-relative ">
<img src="~/#Model.Image" width="425px" height="200px" sizes="cover" style="background-size:cover" />
</div>*#
<form method="post" asp-action="" enctype="multipart/form-data">
<div class="row">
<div class="col-1 ml-4">
<img src="~/#Model.Image" style="width:100%" onclick="myFunction(this);">
</br></br>
#*<img src="~/#Model.Image1" style="width:100%" onclick="myFunction(this);">*#
</br></br>
#if (Model.Photos != null)
{
#foreach (var photo in Model.Photos)
{
#if (photo != null)
{
<img src="~/#photo.Image" alt="Card Image" class="card-img-top" style="height: 120px;" onclick="myFunction(this);"/>
}
}
}
</div>
<div class="col-4 container">
<span onclick="this.parentElement.style.display='none'" class="closebtn">×</span>
<img src="~/#Model.Image" class="active" id="expandedImg" style="width:100%">
<div id="imgtext"></div>
</div>
<div class="col-4">
<h2><i class="fa fa-heart-o" aria-hidden="true"></i> #Model.Name</h2>
<input type="hidden" asp-for="Id" />
</br>
<h4>#Model.Price $</h4></br>
<small class="text-danger">Clearence</small>
<hr>
</br></br></br></br>
<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
<div class="col-sm-12 col-xs-12 spinner-block">
<div class="number-spinner">
<div class="input-group number-spinner">
<b class="mr-4"> <label asp-for="#Model.Quantity"></label></b></br>
<span class="input-group-btn">
<button type="button" class="btn btn-default btn-number btncartsniper" data-type="minus" data-dir="dwn"><span class="fa fa-minus fa-sm"></span></button>
</span>
<input asp-for="#Model.Quantity" class="form-control input-number Snippper_qty" value="0" type="number">
<span class="input-group-btn">
<button type="button" class="btn btn-default btn-number btncartsniper" data-type="plus" data-dir="up"><span class="fa fa-plus fa-sm"></span></button>
</span>
</div>
</div>
</div>
</br>
#*#if (leathers != null)
{*#
<button type="submit" class="btn btn-danger form-control" asp-action="Remove" asp-route-id="#Model.Id">Remove To Cart</button>
#*}*#
#*else
{*#
<input type="submit" value="Add To Cart" class="btn btn-dark form-control" />
#*}*#
</div>
</div>
<div>
<a asp-action="Index" class="btn btn-danger">Back To List</a>
</div>
</form>
#section Scripts{
#{await Html.RenderPartialAsync("_ValidationScriptsPartial");
}
<script>
function myFunction(imgs) {
var expandImg = document.getElementById("expandedImg");
var imgText = document.getElementById("imgtext");
expandImg.src = imgs.src;
imgText.innerHTML = imgs.alt;
expandImg.parentElement.style.display = "block";
}
</script>
}
my output is :
but I want to show my all multiple photos of every product. I failed to understand what's the solution to this.
You should loop through Photos.
That's what the error indicates. It tries to find the enumerator of Shop (Model), while you're intending to loop through photos.
#if (Model.Photos != null)
{
#foreach (var photo in Model.Photos)
{
#if (photo.Image != null)
{
<img src="~/#photo.Image" alt="Card Image" class="card-img-top" style="height: 120px;"/>
}
}
}
You should also include Photos to the product you're returning, otherwise the list will be null.

ASP.NET MVC & C# DropDownListFor set and get value

Revised a bunch of code for ASP.NET MVC, Everywhere example for 500 lines of code at least.
There is a list of the Plane directory (key.plane key). There is a current InstanceC entry (link to the InstanceC.ID_plane directory)
How easy is it to set the value from the controller of the loaded list and read the selected value by the user?
I have the following code:
AccountController.cs
public ActionResult ClickDetails(String slctplane, String Description,String SaveInstance, string AddInstance, string ActivePlane, string RestartInstance, string GeneratePassword, string ResetDB, String DelInstance)
{
if (ActivePlane != null)
{
String id_plane = slctplane; // Выбранный тарифный план
}
}
Model classes:
public class Plane
{
[Key]
public int key_plane { get; set; }
public String Name_plane { get; set; }
public int Num_users { get; set; }
public int Size_db { get; set; }
public int Freq_in_minute { get; set; }
public int Price_usd { get; set; }
public int Price_r { get; set; }
}
public class InstanceC
{
[Key]
public int key_Instance { get; set; }
public String ID_user { get; set; }
public int ID_Server { get; set; }
public String Login_inst { get; set; }
public String Password_inst { get; set; }
public int SetStatusForBroker { get; set; }
public String Description { get; set; }
public int ID_plane { get; set; }
public String Name_plane { get; set; }
public int Price_usd { get; set; }
public int Price_r { get; set; }
public int Num_users { get; set; }
public int RealStatus { get; set; }
public String Name_server { get; set; }
public String Name_domain { get; set; }
}
I have the following markup in Details.cshtml:
#{
ViewBag.Title = "Подробнее/";
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
</head>
<body>
#using (Html.BeginForm("ClickDetails", "Account", FormMethod.Post))
{
<div>
<h2>Подробнее</h2>
<div class="cloud-box">
<div class="row">
<div class="col-md-12">
<h3>Информация о инстанции</h3>
</div>
</div>
#foreach (var i in ViewBag.Details)
{
<div class="row">
<div class="col-md-8">
<table class="table cloud-data">
<tbody>
<tr>
<td>Название</td>
<td>
<input name="Description" type="text" value="#i.Description" class="form-control" aria-label="Sizing example input" aria-describedby="inputGroup-sizing-sm">
<button name="SaveInstance" value="#i.key_Instance" type="submit" class="btn btn-success btn-sm pull-right">
<i class="glyphicon glyphicon-floppy-saved"></i>
Сохранить
</button>
<button name="DelInstance" value="#i.key_Instance" type="submit" class="btn btn-danger btn-sm pull-right">
<i class="glyphicon glyphicon-floppy-saved"></i>
Удалить
</button>
</td>
</tr>
<tr>
<td>Тарифный план</td>
<td>
<button type="button" value="" class="btn btn-success btn-sm pull-right" data-toggle="modal" data-target="#ActivePlaneDlg">
<i class="glyphicon glyphicon-usd"></i>
Активировать
</button>
<div class="modal fade" id="ActivePlaneDlg" tabindex="-1" role="dialog" aria-labelledby="ActivePlaneTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="ActivePlaneTitle">Активация тарифного плана</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
Старый тарифный план будет аннулирован.
Активировать и оплатить новый тарифный план?
</div>
<div class="modal-footer">
<button class="btn btn-secondary" id="ActivePlane" name="ActivePlane" value="id_plane=1">
<i class="glyphicon glyphicon-usd"></i>
Да
</button>
<!--
<input type="submit" name="ActivePlane" value="Сохранить" />
<button type="button" class="btn btn-secondary" data-dismiss="modal">Да</button> -->
<button type="button" class="btn btn-primary" data-dismiss="modal">Нет</button>
</div>
</div>
</div>
</div>
</tr>
<tr>
<td>Статус тарифного плана</td>
<td>
<font color="green">Активирован</font>
</td>
</tr>
<tr>
<td>Дата продления</td>
<td>01.01.2019</td>
</tr>
<tr>
<td>Автопродление</td>
<td>Активировано</td>
</tr>
<tr>
<td>Сервер</td>
<td>#i.Name_domain</td>
</tr>
<tr>
<td>Пользователь</td>
<td>
#i.Login_inst
<button type="button" class="btn btn-cloud btn-cloud-danger btn-sm pull-right" data-toggle="modal" data-target="#RebootInstanceDlg">
<i class="glyphicon glyphicon-repeat"></i>
Перезапуск
</button>
<div class="modal fade" id="RebootInstanceDlg" tabindex="-1" role="dialog" aria-labelledby="RebootInstanceTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="RebootInstanceTitle">Перезагрузка</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
Вы уверены, что хотите перезагрузить инстанс?
</div>
<div class="modal-footer">
<!-- <button type="button" name="RestartInstance" class="btn btn-secondary" data-dismiss="modal">Да</button>
<input type="submit" name="RestartInstance" value="Да" />-->
<button class="btn btn-secondary" id="RestartInstance" name="RestartInstance" value="id_instance=1">
<i class="glyphicon glyphicon-repeat"></i>
Да
</button>
<button type="button" class="btn btn-primary" data-dismiss="modal">Нет</button>
</div>
</div>
</div>
</div>
</td>
</tr>
<tr>
<td>
Пароль
</td>
<td>
#i.Password_inst
<button type="button" class="btn btn-cloud btn-cloud-danger btn-sm pull-right" data-toggle="modal" data-target="#ResetPasswordInstanceDlg">
<i class="glyphicon glyphicon-refresh"></i>
Сгенерировать
</button>
<div class="modal fade" id="ResetPasswordInstanceDlg" tabindex="-1" role="dialog" aria-labelledby="ResetPasswordInstanceTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="ResetPasswordInstanceTitle">Генерация нового пароля</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
Сгенерировать новый пароль?
</div>
<div class="modal-footer">
<button class="btn btn-secondary" id="GeneratePassword" name="GeneratePassword" value="id_plane=2">
<i class="glyphicon glyphicon-refresh"></i>
Да
</button>
<!-- <button type="button" id="GeneratePassword" class="btn btn-secondary" value="id_psw=1" name="GeneratePassword" data-dismiss="modal">Да</button> -->
<!--<input type="submit" name="GeneratePassword" value="Да" />-->
<!--
<button class="btn btn-danger" id="resetdb" name="ResetDB" value="id_plane=1">
<i class="glyphicon glyphicon-trash"></i>
Сброс БД
</button>
>-->
<button type="button" class="btn btn-primary" data-dismiss="modal">Нет</button>
</div>
</div>
</div>
</div>
</td>
</tr>
<!----------------------------Начало: Список доступных портов---------------------------------------------------->
#foreach (var a in ViewBag.PortsDetail)
{
<tr>
<td>Протокол</td>
<td>
#a.NamePort
</td>
</tr>
<tr>
<td>Порт №</td>
<td>
#a.NumPort
</td>
</tr>
<hr>
}
<!---------------------------Конец: Список доступных портов------------------------------------------------------>
</tbody>
</table>
</div>
</div>
<hr>
<div class="row">
<div class="col-md-12">
<h3>Сброс БД</h3>
</div>
<div class="col-md-10">
Это сотрет все сохраненные сообщения в БД.
</div>
<div class="col-md-2">
<form action="https://*/console/9751325/details/reset" class="pull-right" id="reset-form" method="post">
<button class="btn btn-danger" id="resetdb" name="ResetDB" value="id_plane=1">
<i class="glyphicon glyphicon-trash"></i>
Сброс БД
</button>
</form>
</div>
</div>
<hr>
<div class="row">
<div class="col-md-12">
<h3>API</h3>
</div>
</div>
<div class="row">
<div class="col-sm-2">
<strong>API ключ</strong>
</div>
<div class="col-sm-10">
<code>0942387c-de7c-4241-2349da3-e59e4c1afdf6</code>
</div>
</div>
}
</div>
</div>
}
</body>
</html>
You can use Model for form. That allow you setup default values in the controller and handle it from form POST.
Your view model
class PlaneFormModel
{
public string slctplane { get; set; }
public string Description { get; set; }
public string SaveInstance { get; set; }
public string AddInstance { get; set; }
public string ActivePlane { get; set; }
public string RestartInstance { get; set; }
public string GeneratePassword { get; set; }
public string ResetDB { get; set; }
public string DelInstance { get; set; }
public string DropDownValueSelected { get; set; }
public string List<SelectListItem> DropDownValues { get; set; }
}
Your HttpGet controller method
[HttpGet]
public ActionResult ClickDetails()
{
// set view model with dropdown values, also you can use linq from your list of values
// also you can set default value for dropdown
var formModel = new PlaneFormModel
{
DropDownValueSelected = "defaultValue", // <---- value
DropDownValues = new List<SelectListItem>
{
new SelectListItem
{
Key = "key",
Value = "val"
}
}
};
return View(formMmodel);
}
Your HttpPost controller method
[HttpPost]
public ActionResult ClickDetails(PlaneFormModel formModel)
{
// bind model from form also you can use FormCollection instead.
if (!string.IsNullOrEmpty(formModel.DropDownValueSelected))
{
...
}
...
}
And in view simply use razor template helper
#Html.DropDownListFor(m => m.DropDownValueSelected,
Model.DropDownValues,
"Select plane")
Source 1
Source 2
Here is how you do it:
public class TheModel
{
//Can be string, int, Guid, etc. Usually this is the index key.
public int SelectedValue {get;set;}
public List<SelectListItem> TheList {get;set;}
public TheModel()
{
TheList = new List<SelectListItem>();
}
}
For instance you have a model with int type index key. You get get it from the database and insert it on the list.
TheModel model = new TheModel();
using(MigrationDB db = new MigrationDB())
{
model.TheList = db.Plane.Select(m => new SelectListItem()
{
Value = m.PlaneId.ToString(), //Value can hold only string
Text = m.PlaneName //this is the <option>text</option>
}.ToList();
}
This is how you write it in Razor view:
#model TheModel
.....
<form> //You can use Html.BeginForm() to create the form tag.
#Html.DropDownListFor(m => m.SelectedValue, //This will bind the selected option into `SelectedValue` property
Model.TheList, //This will fill up your <option> with the value from the list.
" Select Plane ", //If no value is selected this option will be selected with the value null
new { #class = "custom-class", ...} //Html attribute you want to put here.
);
</form>
So if you select an option from the dropdown box, it will be bound to SelectedValue property.
[HttpPost]
public ActionResult PassTheValue(TheModel model) //Pass the entire model
{
var theSelectedValue = model.SelectedValue;
...
}
You should pass the entire model from the Html page view into the controller.
A reference to Html.BeginForm() source

MVC PartialViews models

I want to use a different model for my partialviews, how to do this? Do you have examples?
<div id="tabs">
<ul>
<li>first tab</li>
<li>second tab</li>
<li>third tab</li>
</ul>
<div id="tabs-1">
#{Html.RenderPartial("FirstTabView", Model)}
</div>
<div id="tabs-2">
#{Html.RenderPartial("SecondTabView", Model)}
</div>
<div id="tabs-3">
#{Html.RenderPartial("ThirdTabView", Model)}
</div>
</div>
You could have sub-models as part of your main model, such as:
public class YourModel
{
public FirstTabModel FirstTab { get; set; }
public SecondTabModel SecondTab { get; set; }
public ThirdTabModel ThirdTab { get; set; }
}
Then you can do:
<div id="tabs-1">
#{Html.RenderPartial("FirstTabView", Model.FirstTab)}
</div>
<div id="tabs-2">
#{Html.RenderPartial("SecondTabView", Model.SecondTab)}
</div>
<div id="tabs-3">
#{Html.RenderPartial("ThirdTabView", Model.ThirdTab)}
</div>

How to get Model List in Controller

I have this form in MVC Razor . The html is quite long so I am posting the area where I want to work
these are the checkboxes which are populated as service packages
The flow is the user will upload a photo and when proceeds to the next step it goes to a page where he will be selecting service for each photo if he has uploaded 3 photos the three photos will be having the same service
listing but different order IDS
I want is that when a user select services for a specific order means a specific order has multiple services . I want to map it so that Model List will be feteched in the Controller and I can easily insert in the database
this is the HTML:
#using (Html.BeginForm("PlaceOrder", "User", FormMethod.Post, new { id="ServiceForm",#class = "form-horizontal
white-clr", role = "form" }))
{
<!--If user has uploaded a single photo or multiple photo -->
<div class="window-for-choose-service">
#if (ViewBag.OrdersList != null)
{
int i= 1;
foreach (var d in ViewBag.OrdersList)
{
<div class="row">
<div class="col-sm-3 col-md-3 col-lg-3">
<img src="#d.ThumbnailPath" class="img-thumbnail img-responsive" width="250">
</div>
<div class="col-sm-9 col-md-9 col-lg-9">
<form class="white-clr" role="form">
<div class="form-group">
<label for="name">Description</label>
<textarea class="form-control" rows="4"></textarea>
</div>
</form>
</div>
<div class="col-sm-12 col-md-12 col-lg-12">
<div class="panel-group" id="accordion_#d.ID">
<div class="panel panel-primary">
#foreach (var Services in ViewBag.ServicePackages)
{
int cID = Services.ID + d.ID;
//int rndNumber = Convert.ToInt32(TimeSpan.FromSeconds(new Random().Next(0,
11221)).ToString("hmsf"));
if (Services.ServiceParentID == null)
{
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion_#d.ID"
href="#collapseOne_#cID">
#Services.ServiceName
</a>
</h4>
</div>
<div id="collapseOne_#cID" class="panel-collapse collapse in">
<div class="panel-body">
<div>
<div class="table-responsive">
#foreach (var child in Services.ft_service_packages1)
{
//string ness = "ServiceSelected[" + #d.ID + "]";
<div style="width:auto;min-width:250px;float:left; padding:4px; display:inline-block">
<div style="float:left;padding-right:5px;">
<label for="inlinecheckbox_#child.ID">
#Html.CheckBoxFor(m => m.ServiceSelect, new { id = "inlinecheckbox_" +
#child.ID, price_attr = "#child.ServicePrice"})
#Html.HiddenFor(m => m.UploadOrderID, new { Value = #d.ID })
</label>
</div>
<div style="padding-left:2px;">#child.ServiceName</div>
</div>
}
</div>
</div>
</div>
</div>
i++;
}
}
</div>
</div>
</div>
<div class="col-sm-12 col-md-12 col-lg-12"><p class="text-right font-blue"><b> Price $
0.0</b></p> </div>
</div>
}
}
The Html is genereting perfectly fine . But the thing is not getting anything in the controller regarding the model
Model is listed below
using System;
using System.Collections.Generic;
public partial class ft_order_itemized
{
public int ID { get; set; }
public int UploadOrderID { get; set; }
public int ServiceSelected { get; set; }
public decimal ServiceCharges { get; set; }
public System.DateTime ServiceSelectedDate { get; set; }
public Nullable<int> OrderID { get; set; }
public bool ServiceSelect { get; set; }
public virtual ft_orders ft_orders { get; set; }
public virtual ft_service_packages ft_service_packages { get; set; }
public virtual ft_uploads_orders ft_uploads_orders { get; set; }
}
My Controller :
[HttpPost]
public ActionResult PlaceOrder(ICollection<ft_order_itemized> Orders)
{
if (Orders.Count > 0)
{
}
// ICollection Allstrings = Orders["Service"].ToList();
return View();
}

Categories

Resources