Binding List with checkbox and post back [duplicate] - asp.net-mvc

This question already has answers here:
Post an HTML Table to ADO.NET DataTable
(2 answers)
Closed 4 years ago.
I have a object that I am binding to my view. This object has a list of items with a boolean field on them. I use this boolean field to bind to a checkbox on the form. I am wanting the user to select the items they want and then submit. This issue is, when it is submitted and passed to the post action, the list of items is null.
Here is the View Model passed into the view.
public class ReAssignVM
{
public string ToUsername { get; set; }
public string FromUsername { get; set; }
public IEnumerable<ReAssignTaskVM> Tasks { get; set; }
} // End of Class
Here is the view:
#model SRM.ClassLibrary.V1.ViewModels.TaskViewModels.ReAssignVM
#{
ViewData["Title"] = "Re-Assign Tasks";
}
<h2>Re-Assign Tasks</h2>
<br />
<div class="row">
<div class="col-md-1 alignRight">
<b>From:</b>
</div>
<div class="col-md-3">
#Model.FromUsername
</div>
<div class="col-md-1 alignRight">
<b>To:</b>
</div>
<div class="col-md-3">
#Model.ToUsername
</div>
</div>
<br />
<form asp-action="ReAssignTasks">
<input type="hidden" asp-for="FromUsername" />
<input type="hidden" asp-for="ToUsername" />
<div include-if="#Model.Tasks != null && Model.Tasks.Count() > 0">
<table class="table table-hover">
<thead class="thead-light">
<tr>
<th></th>
<th>
Status
</th>
<th>
Date
</th>
<th>
Student
</th>
<th>
Description
</th>
</tr>
</thead>
<tbody>
#foreach (var task in Model.Tasks.OrderBy(n => n.TaskDate))
{
<tr>
<td>
#Html.HiddenFor(item => task.TaskID)
#Html.CheckBoxFor(item => task.Assign)
</td>
<td>
#Html.DisplayFor(item => task.TaskStatus)
</td>
<td>
#task.TaskDate.ToLocalTime().ToString("M/d/yyyy")
</td>
<td>
#Html.DisplayFor(item => task.StudentUsername)
</td>
<td>
#Html.DisplayFor(item => task.Description)
</td>
</tr>
}
</tbody>
</table>
<br />
<div class="row">
<div class="col-md-12 alignRight">
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#assignment">
Re-Assign
</button>
</div>
</div>
</div>
<div class="modal fade" id="assignment" tabindex="-1" role="dialog" aria-labelledby="assignmentLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="assignmentLabel">Confirm Re-Assignment of Tasks?</h5>
<button class="close" type="button" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">Selected tasks will be re-assigned from #Model.FromUsername to #Model.ToUsername</div>
<div class="modal-footer">
<button class="btn btn-secondary" type="button" data-dismiss="modal">Cancel</button>
<input type="submit" value="Confirm" class="btn btn-primary" />
</div>
</div>
</div>
</div>
</form>
<div exclude-if="#Model.Tasks != null && Model.Tasks.Count() > 0">
<div class="alert alert-info">
There are no tasks to re-assign.
</div>
</div>
Can someone help me understand how to do this correctly?

You will need to provide the indexing to your Collection items to posted back in the model as Model binder would use those to bind back the objects to action. So you should be using For loop and IList<T> as your model in view :
#for(int i=0; i< Model.Tasks.Count(); i++)
{
#Html.HiddenFor(item => Model.Tasks[i].TaskID)
#Html.CheckBoxFor(item => Model.Tasks[i].Assign)
............
............
}
and in your model change it to be IList<T> as you wouldn't have indexer on IEnumerable<T>:
public class ReAssignVM
{
public string ToUsername { get; set; }
public string FromUsername { get; set; }
public IList<ReAssignTaskVM> Tasks { get; set; }
} // End of Class

Related

How can i render partial view as modal popup on button click?

I am trying to render partial view from controller on button click event to view the details. But unfortunately its not working.
My Controller Action
OwnerController.cs
public IActionResult ShowpopUp(int id) {
var venue = _context.Venues.FirstOrDefault(x=>x.Id==id);
return PartialView(venue);
}
My View All.cshtml
#model List<Venue>
<table class="table table-hover">
<thead>
<th> Property Name </th>
<th colspan="2">Action</th>
</thead>
<tbody>
#foreach(var x in Model)
{
<tr>
<td>
#x.Name
</td>
<td>
<a class="btn btn-default btn-sm" id="#x.Id" onclick="Details(this.id)">Show</a>
</td>
</tr>
}
</tbody>
</table>
<script>
function Details(id)
{
$.get("#Url.Action("ShowpopUp","Owner")/"+id,
function(data) {$('.modal-body').html(data);})
$("#myModal").modal("show");
}
$('#myModal').on('hidden.bs.modal', function(e){
$('.modal-body').html("");
})
}
</script>
myModal
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Details</h4>
</div>
<div class="modal-body">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
The following example should help achieve your requirement of rendering partial view as modal popup using jQuery Ajax, please check it.
All.cshtml
#model IEnumerable<Venue>
#{
ViewData["Title"] = "All";
}
<h1>All</h1>
<table class="table table-hover">
<thead>
<th> Property Name </th>
<th colspan="2">Action</th>
</thead>
<tbody>
#foreach (var x in Model)
{
<tr>
<td>
#x.Name
</td>
<td>
<a class="btn btn-default btn-sm" id="#x.Id" onclick="Details(this.id)">Show</a>
</td>
</tr>
}
</tbody>
</table>
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Details</h4>
</div>
<div class="modal-body">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
#section scripts{
<script>
function Details(id) {
$.get("#Url.Action("ShowpopUp","Owner")/" + id,
function (data) {
$('.modal-body').html(data);
});
$("#myModal").modal("show");
}
</script>
}
ShowpopUp action
public IActionResult ShowpopUp(int id)
{
var venue = _context.Venues.FirstOrDefault(x => x.Id == id);
//specify the name or path of the partial view
return PartialView("_VenueDetail", venue);
}
_VenueDetail.cshtml (partial view under Views/Shared folder)
#model Venue
<h1>Venue Details</h1>
<h2>Id: #Model.Id</h2>
<h2>Name: #Model.Name</h2>
Test Result

Display different details in a modal when clicking a button in a list MVC5

Im trying to display more details about a restaurant when you click on a specific restaurant on a list. Currently I am displaying a list of restaurants in a list with a button saying more info. I want this information to show up in a modal
When I click on different restaurants in the list the information that is displayed in the modal is the same for each restaurant.
Here is the code that I'm using
#foreach (var item in Model)
{
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<center><h2 id="modalTitle" class="modal-title">#item.RestaurantName</h2></center>
</div>
<div class="modal-body">
<ul class="details">
<li class="modalListItem">
<i class="fa fa-info-circle fa-5" aria-hidden="true"></i>
<span>#item.RestaurantDescription</span>
</li>
<li class="modalListItem">
<i class="fa fa-envelope fa-5" aria-hidden="true"></i>
<span>#item.RestaurantEmailAddress</span>
</li>
<li class="modalListItem">
<i class="fa fa-clock-o fa-5" aria-hidden="true"></i>
<span>Opens from #item.OpeningTime.TimeOfDay - #item.ClosingTime.TimeOfDay</span>
</li>
<li class="modalListItem">
<i class="fa fa-list-alt fa-5" aria-hidden="true"></i>
<span>#item.FurtherDetails</span>
</li>
<li class="modalListItem">
<i class="fa fa-map-marker fa-5" aria-hidden="true"></i>
<span>#item.RestaurantAddress #Html.ActionLink("Directions", "GetDirections", new { id = item.RestaurantID }, new { #class = "btn btn-primary btn-xs" })</span>
</li>
<li class="modalListItem">
<i class="fa fa-bookmark-o fa-5" aria-hidden="true"></i>
<span>Would you like to make a reservation? #Html.ActionLink("Reserve", "CreateReservation", new { id = item.RestaurantID }, new { #class = "btn btn-success btn-xs" })</span>
</li>
</ul>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
}
Here is the code which has the button
<table id="restaurantTable" class="table table-striped table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<th>Restaurant Name</th>
<th>Type Of Restaurant</th>
<th>Phone</th>
<th>Address</th>
<th>Options</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.RestaurantName)
</td>
<td>
#Html.DisplayFor(modelItem => item.RestaurantType)
</td>
<td>
#Html.DisplayFor(modelItem => item.RestaurantPhoneNo)
</td>
<td>
#Html.DisplayFor(modelItem => item.RestaurantAddress)
</td>
<td>
<button type="button" class="btn btn-info btn-xs" data-toggle="modal" data-target="#myModal">More info</button> |
#*#Html.ActionLink("More Info", "Details", new { id = item.RestaurantID }, new { #class = "btn btn-info btn-xs" }) |*#
#Html.ActionLink("Reserve", "CreateReservation", new { id = item.RestaurantID }, new { #class = "btn btn-success btn-xs" }) |
#Html.ActionLink("Directions", "GetDirections", new { id = item.RestaurantID }, new { #class = "btn btn-primary btn-xs" }) |
#Html.ActionLink("Review", "Review", new { id = item.RestaurantID }, new { #class = "btn btn-primary btn-xs" })
</td>
</tr>
}
</tbody>
</table>
}
Calling bootbox with bootstrap 3 modal.
Example:
Model:
public class Restaurant
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public string Website { get; set; }
public string Address { get; set; }
}
Controller:
public class RestaurantController : Controller
{
public ActionResult Index()
{
var model =new List<Restaurant>();//load data from database
return PartialView(model);
}
[HttpGet]
public ActionResult Delails(int id)
{
var model = new Restaurant();//load data from database by RestaurantId
return PartialView(model);
}
}
View:
index.cshtml:
#model IEnumerable<Restaurant>
<table id="dataTable" class="table table-bordered table-hover">
<thead>
<tr>
<th>
#Html.DisplayNameFor(model => model.Name)
</th>
<th>
#Html.DisplayNameFor(model => model.Website)
</th>
<th>
#Html.DisplayNameFor(model => model.Address)
</th>
<th style="max-width:100px">Actions</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr class="dataItem">
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.Website)
</td>
<td>
#Html.DisplayFor(modelItem => item.Address)
</td>
<td>
<span class="btn btn-xs btn-primary btnEdit" id="edit_#item.Id" onclick="createModal('#Url.Action("Details", "Restaurant" , new {id=item.Id })')">Details</span>
</td>
</tr>
}
</tbody>
</table>
<div class="modal fade" id="myModal" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" data-backdrop="static" data-keyboard="false">
<div class="modal-dialog">
<div class="modal-content" id="modelContent">
</div>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootbox.js/4.4.0/bootbox.min.js">
script>
<script>
function createModal(url){
$('#modelContent').load(url);
$('#myModal').modal('show');
}
</script>
Details.cshtml:
#model Restaurant
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true"><i class="fa fa-times-circle"></i></span>
</button>
<h4 class="modal-title">Details Record</h4>
</div>
<div class="modal-body">
<div>
<table class="table table-bordered">
<tr>
<td>#Html.DisplayNameFor(model => model.Name)</td>
<td>#Html.DisplayFor(model => model.Name)</td>
</tr>
<tr>
<td>#Html.DisplayNameFor(model => model.Website)</td>
<td>#Html.DisplayFor(model => model.Website)</td>
</tr>
<tr>
<td>#Html.DisplayNameFor(model => model.Address)</td>
<td>#Html.DisplayFor(model => model.Address)</td>
</tr>
</table>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default"
data-dismiss="modal">
Close
</button>
</div>
I think it's helpful for you.

ReleaseDate data not persisting in Edit View ASP.NET MVC

I have the following Index view:
#model IEnumerable<MVCMovie3.Models.Movie>
#{
ViewData["Title"] = "Index";
}
<h2>Index</h2>
<p>
<a asp-action="Create">Create New</a>
</p>
<form asp-controller="Movies" asp-action="Index">
<p>
Title: <input type="text" name="SearchString">
<input type="submit" value="Filter" />
</p>
</form>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.Title)
</th>
<th>
#Html.DisplayNameFor(model => model.Genre)
</th>
<th>
#Html.DisplayNameFor(model => model.ReleaseDate)
</th>
<th>
#Html.DisplayNameFor(model => model.Rating)
</th>
<th>
#Html.DisplayNameFor(model => model.Price)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.Title)
</td>
<td>
#Html.DisplayFor(modelItem => item.Genre)
</td>
<td>
#Html.DisplayFor(modelItem => item.ReleaseDate)
</td>
<td>
#Html.DisplayFor(modelItem => item.Rating)
</td>
<td>
#Html.DisplayFor(modelItem => item.Price)
</td>
<td>
<a asp-action="Edit" asp-route-id="#item.ID">Edit</a> |
<a asp-action="Details" asp-route-id="#item.ID">Details</a> |
<a asp-action="Delete" asp-route-id="#item.ID">Delete</a>
</td>
</tr>
}
</table>
And here is the "Edit" view listed in the Index view:
#model MVCMovie3.Models.Movie
#{
ViewData["Title"] = "Edit";
}
<h2>Edit</h2>
<form asp-action="Edit">
<div class="form-horizontal">
<h4>Movie</h4>
<hr />
<div asp-validation-summary="ValidationSummary.ModelOnly" class="text-danger"></div>
<input type="hidden" asp-for="ID" />
<div class="form-group">
<label asp-for="Genre" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="Genre" class="form-control" />
<span asp-validation-for="Genre" class="text-danger" />
</div>
</div>
<div class="form-group">
<label asp-for="Price" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="Price" class="form-control" />
<span asp-validation-for="Price" class="text-danger" />
</div>
</div>
<div class="form-group">
<label asp-for="Rating" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="Rating" class="form-control" />
<span asp-validation-for="Rating" class="text-danger" />
</div>
</div>
<div class="form-group">
<label asp-for="ReleaseDate" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="ReleaseDate" class="form-control" />
<span asp-validation-for="ReleaseDate" class="text-danger" />
</div>
</div>
<div class="form-group">
<label asp-for="Title" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="Title" class="form-control" />
<span asp-validation-for="Title" class="text-danger" />
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
</form>
<div>
<a asp-action="Index">Back to List</a>
</div>
#section Scripts {
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/lib/jquery-validation/dist/jquery.validate.min.js"></script>
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"></script>
My problem is that everything shows fine on the Index view but when I click the link to bring up the Edit view the release date is gone. Any suggestions?
Also, here is my model that I generated the views off of:
using System;
using System.ComponentModel.DataAnnotations;
namespace MVCMovie3.Models
{
public class Movie
{
public int ID { get; set; }
public string Title { get; set; }
public string Genre { get; set; }
public string Rating { get; set; }
public decimal Price { get; set; }
[Display(Name = "ReleaseDate")]
[DataType(DataType.Date)]
public DateTime ReleaseDate { get; set; }
}
}
In playing around with it I've found that if I take off the [Display(Name.... and [DisplayType.... parts then it runs fine, except now my release date is not in the format I want it to be in. Help please.....
For now the only fix that i have found is to change the following code:
[Display(Name = "Release Date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
public DateTime ReleaseDate { get; set; }
To:
[Display(Name = "Release Date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}")]
public DateTime ReleaseDate { get; set; }
This is, to remove the ApplyFormatInEditMode = true from the DisplayFormat line.
No the less as i mentioned before this would make the DatePicker field to show a date format in the form dd-MM-yyyy (the default one) and not the one you selected.

HttpPostedFileBase is coming null

below is the code in my View
#using ClaimBuildMVC.Models
#model IEnumerable<Asset>
#{
ViewBag.Title = "AssetsPage";
Layout = "~/Views/Shared/_SuperAdminLayout.cshtml";
}
<script type="text/javascript">
</script>
#using (Html.BeginForm("AssetsPage", "SuperAdmin", FormMethod.Post,new{enctype = "multipart/form-data"}))
{
<div class="Content-inner-pages">
<div class="TopHeading TopHeading2">
<h2>Assets</h2>
<a class="CreateBtn AssetsBtn" href="Javascript:void(0);" onclick="javascript:$('#hdnIsNew').val('1')">Add Asset</a>
<div class="clearfix"></div>
</div>
<input type="hidden" id="hdnIsNew" value="1" />
<input type="hidden" id="hdnRecId" />
<!-- Slide Popup panel -->
<div class="cd-panel from-right AddAssetForm">
<header class="cd-panel-header">
<h3>Add Asset</h3>
Close
</header>
<div class="cd-panel-container">
<div class="cd-panel-content">
<div class="form-horizontal form-details popup-box">
<div class="form-group">
<label class="col-md-5 control-label">
Asset Title
</label>
<div class="col-md-7">
<input type="text" id="txtTitle" name="txtTitle" class="form-control">
</div>
</div>
<div class="form-group">
<label class="col-md-5 control-label">Description</label>
<div class="col-md-7">
<textarea id="txtDesc" class="form-control" cols="5" rows="5"></textarea>
</div>
</div>
<div class="form-group">
<label class="col-md-5 control-label">Attachment</label>
<div class="col-md-7">
<input type="file" id="file" class="custom-file-input">
</div>
</div>
<div class="form-group">
<div class="col-md-7 col-md-offset-5">
<input type="submit" value="Save" name="actionType" class="btn-class btn-success">
</div>
</div>
</div>
</div>
</div>
</div>
<div class="box">
<div class="box-content Custom-DataTable">
<table id="AdministationAssets" class="table table-hover dt-responsive CustomDatable AdministationAssetsTable" cellspacing="0" width="100%">
<thead>
<tr>
<th style="width:5%;">Assets</th>
<th style="width:15%;">
#Html.DisplayNameFor(model =>model.Title)
</th>
<th style="width:50%;">
#Html.DisplayNameFor(model =>model.Description)
</th>
<th style="width:8%;">Options</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td></td>
<td>
#Html.DisplayFor(modelItem => item.Title)
</td>
<td>
#Html.DisplayFor(modelItem =>item.Description)
</td>
<td>
#Html.ActionLink("Edit", "AddEditRecord", new{ id = item.ID }, new { #class = "ActionEdit AssetEdit", onclick ="javascript:GetEditDetails(" + item.ID + ");" })
#Html.ActionLink("Delete", "AssetDelete", new{ id = item.ID }, new { #class = "ActionDelete", onclick = "return confirm('Are You Sure delete this record?');", })
</td>
</tr>
}
</tbody>
</table>
</div>
</div>
and below is my controller that what i want to call when click on save button but 'img' is coming as null and after searching in google i found that to add #using(Html.BeginForm()) but still it is coming as null
[HttpPost]
public ActionResult AssetsPage(Asset ast, HttpPostedFileBase file)
{
using (GenericUnitOfWork gc = new GenericUnitOfWork())
{
if (HttpContext.Request.Files.Count > 0)
{
ast.ContainerName = "reports";
ast.BlobName = HttpContext.Request.Files[0].FileName;
ast.BlobUrl = BlobUtilities.CreateBlob(ast.ContainerName, ast.BlobName, null, GetStream(HttpContext.Request.Files[0].FileName));
ast.FileName = HttpContext.Request.Files[0].FileName;
}
gc.GetRepoInstance<Asset>().Add(ast);
gc.SaveChanges();
}
return RedirectToAction("AssetsPage");
}
Not able to find the solution. Please help or give some reference if possible.
Asp.Net MVC default model binding works with name attribute, So add name="file" attribute with input type="file" as shown :-
<input type="file" name="file" id="file" class="custom-file-input">

How can I send to view a DTO and Model

My Dto
public class homePageDTO
{
public IEnumerable<Yazi> yazi { get; set; }
public IEnumerable<Yorum> yorum { get; set; }
}
My ActionMethod
public ActionResult Post(int pid)
{
homePageDTO obj = new homePageDTO();
obj.yazi = ent.Yazi.Where(x=>(x.YaziID==(int)pid)).ToList();
obj.yorum = ent.Yorum.Where(x => (x.YorumNo == (int)pid)).ToList();
return View(obj);
}
My multi model
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApplication15.Models
{
public class BigViewModel
{
public homePageDTO home { get; set; }
public Yorum Yorrum { get; set; }
}
}
My View
#model WebApplication15.Models.BigViewModel
#{
ViewBag.Title = "Post";
Layout = "~/Views/Shared/_Layout.cshtml";
}
#foreach (var item in Model.home.yazi)
{
<header class="intro-header" style="background-image: url('/content/img/post-bg.jpg')">
<div class="container">
<div class="row">
<div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
<div class="site-heading">
<h1>#item.YaziUstBaslik</h1>
<hr class="small">
<span class="subheading"><b>#item.YaziAltBaslik</b></span>
</div>
</div>
</div>
</div>
</header>
<!-- Post Content -->
<article>
<div class="container">
<div class="row">
<div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
<p>#item.Yazi1</p>
<p class="alert alert-warning">Yollayan #item.YazarKullaniciAdi-#item.YaziTarih</p>
</div>
</div>
</div>
</article>
<hr>
}
<h2>Index</h2>
<table class="table">
<tr>
<th>
Yorum
</th>
<th>
Yorum Sahibi
</th>
<th>
Tarihi
</th>
<th></th>
</tr>
#foreach (var x in Model.home.yorum)
{
<tr>
<td>
#x.Yorum1
</td>
<td>
#x.YorumSahibi
</td>
<td>
#x.YorumTarihi
</td>
</tr>
}
</table>
<div class="container">
<h2>Modal Example</h2>
<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Yeni Yorum</button>
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Selam</h4>
</div>
<div class="modal-body">
<p>nasdsadasdasda</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Yolla</button>
</div>
</div>
</div>
</div>
</div>
ERROR
The model item passed into the dictionary is of type 'WebApplication15.Models.homePageDTO', but this dictionary requires a model item of type 'WebApplication15.Models.BigViewModel'.
It was dissolved
My new action method
public ActionResult Post(int pid)
{
BigViewModel bigyorum = new BigViewModel();
bigyorum.home = new homePageDTO();
bigyorum.home.yazi = ent.Yazi.Where(x=>(x.YaziID==(int)pid)).ToList();
bigyorum.home.yorum = ent.Yorum.Where(x => (x.YorumNo == (int)pid)).ToList();
return View(bigyorum);
}
you need to change the model that is related in the view, in the first line of the view set this:
#model WebApplication15.Models.homePageDTO
I think homePageDTO is in the namespace WebApplication15.Models, if not, put the correct namespace.
With your update:
In your action, you are passing an object of type homePageDTO:
public ActionResult Post(int pid)
{
homePageDTO obj = new homePageDTO();
obj.yazi = ent.Yazi.Where(x=>(x.YaziID==(int)pid)).ToList();
obj.yorum = ent.Yorum.Where(x => (x.YorumNo == (int)pid)).ToList();
return View(obj);
}
and in your view, yo have:
#model WebApplication15.Models.BigViewModel
There are differents objects, the type of the object that you pass from the action to the view, must be the same that you declare in the view.
So, change the action to return a BigViewModel or change in your view to homePageDTO

Resources