Tabs in asp.net mvc - asp.net-mvc

I have three tabs on page and one user control.
I want to RenderPartial on each tab.
<script type="text/javascript">
$(document).ready(function() {
$("#tabs").tabs();
});
</script>
<div id="tabs">
<ul>
<li>Text 1</li>
<li>Text 2</li>
<li>Text 3</li>
</ul>
<div id="tabs-1">
<% Html.RenderPartial("usercontrol", Model); %>
</div>
<div id="tabs-2">
<% Html.RenderPartial("usercontrol", Model); %>
</div>
<div id="tabs-3">
<% Html.RenderPartial("usercontrol", Model); %>
</div>
</div>
how can i get values from different tabs.

If you want to recognize tab, which sent data back, you'll have to pass additional parameter to every partial view:
public class UserControlModel
{
ModelType ParentModel { get; set; }
string ID { get; set; }
}
<div id="tabs-1">
<% Html.RenderPartial("usercontrol", new UserControlModel { ParentModel = Model, ID = "tab1" }); %>
</div>
<div id="tabs-2">
<% Html.RenderPartial("usercontrol", new UserControlModel { ParentModel = Model, ID = "tab2" }); %>
</div>
<div id="tabs-3">
<% Html.RenderPartial("usercontrol", new UserControlModel { ParentModel = Model, ID = "tab3" }); %>
</div>
Then use Html.Hidden to store value in form in partial view and by checking value in post method, you can recognize tab.
Your partial view will have to inherit from ViewUserControl<TaskEditModel>:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<TaskEditModel>" %>

Related

The model item passed into the dictionary is of type 'System.Collections.Generic.List`1 .

Welcome,
I have several pictures, when I click on one it should take me to another window of this picture.
But this does not happen, but I get this window with this error message:
The model item passed into the dictionary is of type
'System.Collections.Generic.List`1[FirstProject.Models.student]', but
this dictionary requires a model item
I will put the codes related to the problem:
HomeController
using FirstProject.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace FirstProject.Controllers
{
public class HomeController : Controller
{
level_8_coursesEntities db = new level_8_coursesEntities();
public ActionResult Index()
{
List<level_8_courses> list = db.level_8_courses.ToList();
return View(list);
}
public ActionResult Numberofstudents()
{
List<level_8_courses> list = db.level_8_courses.ToList();
return View(list);
}
public ActionResult Students(int? id)
{
List<student> studentlist = null;
if (id.HasValue)
{
studentlist = db.students.Where(x => x.corId == id).ToList();
}
else
{
// Set the `studentlist` to some default value when `id` doesn't defined or not a number.
studentlist = null;
}
return View(studentlist);
}
Number of students.chtml
#{
ViewBag.Title = "Numberofstudents";
}
#using FirstProject.Models;
#model List<level_8_courses>
<div class="container">
<section class="page-section portfolio" id="portfolio">
<div class="container">
<!-- Portfolio Section Heading-->
<h2 class="page-section-heading text-center text-uppercase text-secondary mb-0">Numberofstudents</h2>
<!-- Icon Divider-->
<div class="divider-custom">
<div class="divider-custom-line"></div>
<div class="divider-custom-icon"><i class="fas fa-star"></i></div>
<div class="divider-custom-line"></div>
</div>
<!-- Portfolio Grid Items-->
<div class="row justify-content-center">
<!-- Portfolio Item 1 data-bs-target="#portfolioModal" -->
#foreach (var item in Model)
{
<div class="col-md-6 col-lg-4 mb-5">
<a href="/Home/students/#item.Id">
<div class="portfolio-item mx-auto" data-bs-toggle="modal">
<div class="portfolio-item-caption d-flex align-items-center justify-content-center h-100 w-100">
<div class="portfolio-item-caption-content text-center text-white"><i class="fas fa-plus fa-3x"></i></div>
</div>
<img class="img-fluid" style="height:100px;width:100px" src="#item.photo" alt="" />
</div>
</a>
<div style="text-align:center">
<h5>#item.corName</h5>
</div>
</div>
}
</div>
</div>
</section>
</div>
Students.chtml
#using FirstProject.Models;
#model List<student>
<div class="container">
<div class="row">
#foreach (var item in Model)
{
<div class="card col-sm-6">
<div class="card" style="width: 18rem;">
<img class="card-img-top" src="..." alt="Card image cap">
<div class="card-body">
<h5 class="card-title">#item.sName</h5>
<p class="card-text">#item.sEmail</p>
</div>
</div>
</div>
}
</div>
</div>
Thanks
fix students action, if you don't have any students create an empty list as a model, and remove studentlist = null;
List<student> studentlist = null
if (id!=null && id > 0)
studentlist = db.students.Where(x => x.corId == id).ToList();
if(studentlist==null) studentslist= new List<student>();
return View(studentlist);

How to add div tag after fixed number of iteration in mvc

I am using Umbraco (mvc) with bootstrap.My template use partial view to load name and job title of staff in template. How I can add div (new row) after 4 iteration.
#inherits Umbraco.Web.Mvc.UmbracoTemplatePage
#{
var selection = Model.Content.Site().FirstChild("aboutUs").Children("staff")
.Where(x => x.IsVisible());
}
#foreach(var item in selection){
<div class="row team">
<div class="col-sm-3">
<div class="thumbnail-style">
<h3><a>#item.GetPropertyValue("fullName")</a> <small>#item.GetPropertyValue("jobTitle")</small></h3>
</div>
</div>
</div>
}
Difficulty:
I want to add
<div class="row team">
</div>
after every 4th
<div class="col-sm-3">
</div>
A more easier solution would be the use of the Umbraco helper method InGroupsOf(columns).
#foreach(var group in selection.InGroupsOf(4))
{
<div class="row team">
#foreach(var item in group)
{
<div class="col-sm-3">
<div class="thumbnail-style">
<h3><a>#item.GetPropertyValue("fullName")</a> <small>#item.GetPropertyValue("jobTitle")</small></h3>
</div>
</div>
}
</div>
}
Try:
#foreach(var obj in selection.Select((item, index) => new {item, index}))
{
if(obj.index==0||obj.index%4==0){
#Html.Raw("<div class='row team'>")
}
<div class="col-sm-3">
<div class="thumbnail-style">
<h3><a>#obj.item.GetPropertyValue("fullName")</a> <small>
#obj.item.GetPropertyValue("jobTitle")</small></h3>
</div>
</div>
#if((obj.index+1)%4==0||obj.index+1==selection.Count()){
#Html.Raw("</div>")
}
}
- It will works, Use for loop instead of foreach loop and then just add logic
#for (int i = 1; i < selection.Count; i++)
{
var item = selection[i];
<div class="row team">
<div class="col-sm-3">
<div class="thumbnail-style">
#if (i % 5 == 0)
{
<div class="col-sm-3">
<h4 >D #item</h4> <!--your actual html code-->
</div>
}
else
{
<h4 style="color:red">D #item</h4> <!--your actual html code-->
}
</div>
</div>
</div>
}

How to get descendants from current page in Umbraco 7?

I have a document type with alias dArticlesMain, and for this page I have the following structure.
dArticlesMain
dArticlesCategory1
Article1
Article2
Article3
dArticlesCategory2
Article1
Article2
Article3
dArticlesCategory3
Article1
Article2
Article3
I'm on dArticlesMain and i want to display all the descendants (Articles) and skip it's childrens (dArticlesCategory)
I have this code which display all the childrens (dArticlesCategory) and the descendants (Articles) also when i use the Article properties it's through an error.
<ul>
#foreach(var page in Model.Content.Descendants())
{
<li>#page.Name</li>
}
</ul>
I have got this code but i can't display by Article properties like articleText or articleImage.
<ul>
#foreach(var page in Model.Content.DescendantsOrSelf().OfTypes("Article"))
{
<li>#page.Name</li>
}
</ul>
I have figured it out, and here's my code...
#{
var rootNode = CurrentPage.AncestorOrSelf(1);
var articlesParent = rootNode.Children("dArticlesMain").FirstOrDefault();
<div class="row">
#foreach (var article in articlesParent.Descendants("article").Where("Visible").RandomOrder())
{
<div class="col-sm-6 col-md-3">
<div class="thumbnail">
<a href="#article.Url">
<img src="#article.articlePhoto" alt="#article.articleName" />
</a>
<div class="caption">
<a class="h4" href="#article.Url">#article.articleName</a>
#Umbraco.Truncate(article.articleFullText, 100)
</div>
</div>
</div>
}
</div>
}

how to show video file in mvc when i have a physical path in my table

i have physical path of video in my database table how can i show those video files in my mvc view. can anyone give me an idea.
here is my service layer in which i am getting all the values in a list
public IEnumerable<Marketing> GetMarketingContents()
{
return fDbContext.Marketings.ToList();
}
view model
public class ViewModelMarketing
{
public List<Marketing> MarketVideo { get; set; }
}
here is my controller in which i am filtering the videos and passing it to the partial view.
public ActionResult MarketingListVideos()
{
ViewModelMarketing viewModelMarketing = new ViewModelMarketing();
var debs = from s in iMarketingService.GetMarketingContents()
select s;
viewModelMarketing.MarketVideo = debs.Where(t => t.MarketingType.Equals("v") && t.Active != false).ToList();
return PartialView("_GetVideos", viewModelMarketing);
}
here is my partial view named _GetVideos
#model PreFlight.ViewModels.ViewModelMarketing
#foreach (var item in Model.MarketVideo)
{
<div class="list-group">
<div class="row">
<div class="col-md-1 col-xs-1">
<i class="glyphicon glyphicon-facetime-video" style="color: black"></i>
</div>
<div class="col-md-5 col-xs-5">
#item.Content
</div>
<div class="col-md-5 col-xs-5">
#item.Active
</div>
</div>
</div>
}
here is my main view in which i am rendering this partial view
#model PreFlight.ViewModels.ViewModelMarketing
<div class="panel-body">
<div class="list-group">
#Html.Action("MarketingListVideos", "Marketing")
</div>
</div>
now i am getting the list of videos in my view. how can i show it as a video files. can any one help me to get this. any help will be appreciated.
You can use HTML5 :
#foreach (var item in Model.MarketVideo)
{
<div class="list-group">
<div class="row">
<div class="col-md-1 col-xs-1">
<i class="glyphicon glyphicon-facetime-video" style="color: black"></i>
</div>
<div class="col-md-5 col-xs-5">
#item.Content
</div>
<div class="col-md-5 col-xs-5">
#item.Active
</div>
<video controls="controls">
<source src="#item.YourVideoPath" type="video/mp4" />
</video>
</div>
</div>
}
And for best pratices you should espeficy the correct extension:
<video controls="controls">
<source src="#item.YourVideoPath" type="video/#item.VideoExtension" />
</video>
Here some referece:
http://www.w3schools.com/html/html5_video.asp
Note: just remember that not all browsers supports the video tag, but a good amount does

Display Error message on same view

I'm trying to delete a record from the database using MVC 2. currently delete function works fine but there are some records with foreign key relations so i don't wont them to be deleted and when user try to delete such a record i want to show a error message on the delete view without navigating to another view.
Controller:
[HttpPost]
public ActionResult Delete(int id, FormCollection collection)
{
try
{
// TODO: Add delete logic here
StockRepository rep = new StockRepository();
Stock stock = rep.GetStock(id);
rep.Delete(stock);
rep.Save();
return RedirectToAction("Index");
}
catch
{
//need to display an error message if unable to delete
return View();
}
}
View:
<h2>Delete</h2>
<h3>Are you sure you want to delete this?</h3>
<fieldset>
<legend>Fields</legend>
<div class="display-label">StockID</div>
<div class="display-field"><%: Model.StockID %></div>
<div class="display-label">ClientName</div>
<div class="display-field"><%: Model.ClientName %></div>
<div class="display-label">ItemName</div>
<div class="display-field"><%: Model.ItemName %></div>
<div class="display-label">ItemCount</div>
<div class="display-field"><%: Model.ItemCount %></div>
<div class="display-label">Price</div>
<div class="display-field"><%: String.Format("{0:F}", Model.Price) %></div>
<div class="display-label">OtherExpences</div>
<div class="display-field"><%: String.Format("{0:F}", Model.OtherExpences) %></div>
<div class="display-label">TotalStockValue</div>
<div class="display-field"><%: String.Format("{0:F}", Model.TotalStockValue) %></div>
<div class="display-label">DeliveryDate</div>
<div class="display-field"><%: String.Format("{0:d}", Model.DeliveryDate) %></div>
<div class="display-label">Description</div>
<div class="display-field"><%: Model.Description %></div>
</fieldset>
<% using (Html.BeginForm()) { %>
<p>
<input type="submit" value="Delete" /> |
<%: Html.ActionLink("Back to List", "Index") %>
</p>
<% } %>
Using Viewdata
View
<%
if (ViewData["dbError"] != null)
{
%>
//display ViewData dbError
<%
}
%>
Controllor
[HttpPost]
public ActionResult Delete(int id, FormCollection collection)
{
try
{
// TODO: Add delete logic here
StockRepository rep = new StockRepository();
Stock stock = rep.GetStock(id);
rep.Delete(stock);
rep.Save();
return RedirectToAction("Index");
}
catch
{
//need to display an error message if unable to delete
**ViewData["dbError"] = "Error message here";**
return View();
}
}

Resources