MVC dropdownlist filter onchange - asp.net-mvc

I have an index view with dropdownlist filter. Whenever a new item is selected from the dropdownlist, the index page will display data based on groupid. Here is the view
#using (Html.BeginForm("index", "service"))
{
<div class="row">
<div class="navbar navbar-default">
<div class="navbar-header" style="margin:10px 0px 5px 5px">
<div class="col-lg-12">
<a id="btnSAdd" class="btn btn-info glyphicon glyphicon-plus" href="/AppName/Service/Create"> Create New </a>
<button type="submit" id="btnSSave" class="btn btn-info glyphicon glyphicon-floppy-save"> Save </button>
</div>
</div>
<div class="navbar-collapse collapse navbar-responsive-collapse" style="margin:10px 0px 5px 5px">
<div class="input-group">
<span class="navbar" style="font-family:'Californian FB';font-weight:bold">Select a Menu:</span>
#Html.DropDownList("GroupID", new SelectList(ViewBag.TopMenuList, "GroupID", "GroupName"),
new { #class = "btn btn-info dropdown-toggle", #style = "border-color:grainsboro", #onchange = "window.location = 'service/index?groupid=' + this.value" })
</div>
</div>
</div>
</div>
<div class="row">
<div id="drag" class="panel-body">
<table class="table table-hover" style="width:auto" id="sTable">
<colgroup>
<col width="30" />
<col width="250" />
<col width="250" />
<col width="200" />
</colgroup>
<thead>
<tr>
<th> </th>
<th>Parent Group</th>
<th>Service Name</th>
<th>Service Description</th>
</tr>
</thead>
<tbody>
#for (int i = 0; i < Model.Count; i++)
{
<tr class="orderedrow">
<td class="rowhandler">
<div class="drag rowdip">#Html.HiddenFor(model => model[i].ServiceID)</div>
</td>
<td>#Html.DropDownListFor(model => model[i].GroupID, new SelectList(Model[i].ParentServiceGroupList, "GroupID", "GroupName", Model[i].GroupID))</td>
<td>#Html.EditorFor(model => model[i].ServiceName, new { htmlAttributes = new { #style = "width:250px" } })</td>
<td>#Html.EditorFor(model => model[i].ServiceDescription, new { htmlAttributes = new { #style = "width:250px" } })</td>
</tr>
}
</tbody>
</table>
</div><!--End Div drag-->
</div><!--End of row-->
}
RouteConfig.cs
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
The problem I am having is the filter works only the first time as
http://localhost/AppName/service/index?groupid=4
. The second time, it seems to append my url as
http://localhost/AppName/service/service/index?groupid=5
How do I fix this?
Thanks

Try to change this
#onchange = "window.location = 'service/index?groupid=' + this.value"
to this
#onchange = "window.location = '/service/index?groupid=' + this.value"

Related

MVC form moves when it is validated

I have created an MVC form.
Everything is working fine, however, when error messages are displayed, they move form to the left.
This is a code:
<div style="text-align: center; padding-top: 40px;">
<h2 style="color: white;">Title</h2>
#using (Html.BeginForm())
{
<div>
#Html.EditorForModel()
<p><input type="submit" class="k-primary" value="Log In" /></p>
</div>
}
</div>
This is my Edit Template:
<div style="margin: 0px auto; text-align: center; display: table;">
<table>
<tr>
<td>#Html.EditorFor(x => x.UserName, new { htmlAttributes = new { #class = "form-control", placeholder = "username" } })</td>
<td>#Html.ValidationMessageFor(x => x.UserName,"", new { #class = "text-danger"})</td>
</tr>
<tr>
<td>#Html.EditorFor(x => x.Password, new { htmlAttributes = new { #class = "form-control", placeholder = "password" } })</td>
<td>#Html.ValidationMessageFor(x => x.Password)</td>
</tr>
<tr style="height:70px;">
<td colspan="2"><input type="submit" class="k-primary" value="Log In" /></td>
</tr>
</table>
</div>
This is the generated code:
he form as position:fixed. It did not fix it
What am I missing, so the form does not move anywhere and stays fixed?

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.

How Do I solve "Sequence contains no elements" error

I have an index view that gets a parameter from an Action link. Sometimes there won't be a record and in that case I want the #Model.Count to simply show 0. That was working just fine. I then need a button to pass that same parameter to a Create view to add a new (initial) record and display on the previous index page. This is where I hit a problem.
<input class="btn btn-sm btn-info" type="button" value="Add To Roster" onclick=" location.href = '#Url.Action("Create", "Enrollments", new {enrollments_CurrentSites = Model.First().Enrollments_CurrentSite}, null)' "/>
If there is no record, instead of showing a count of 0, I now get an error "Sequence Contains No Elements". I thought that maybe there is a null Model and tried to check for it, but no go. If there is one or more records, then the Count is displayed and the "Add To Roster" button performs as expected.
Can someone help me solve this error?
Error Code:
#model IEnumerable<SlamJammersData.Model.Enrollments>
#{
ViewBag.Title = "Rosters";
}
<div class="container-fluid">
<div class="panel panel-info">
<div class="panel-body">
<div class="container">
<div class="row">
#if (Model == null)
{
<h4>Roster count: 0</h4>
}
else
{
<p>
<h4>Roster count: #Model.Count() </h4>
</p>
}
</div>
</div>
</div>
<div class="panel-footer">
<div class="row">
<div class="col-sm-2">
<p>
<input class="btn btn-sm btn-info" type="button" value="Add To Roster" onclick=" location.href = '#Url.Action("Create", "Enrollments", new {enrollments_CurrentSites = Model.First().Enrollments_CurrentSite}, null)' "/>
</p>
</div>
<div class="col-sm-2">
<div class="col-sm-2">
<input type="button" class="btn btn-default btn-sm" value="Attendance" />
</div>
</div>
</div>
</div>
<div class="container">
<div class="row">
<div>
<table class="table table-striped table-condensed table-responsive">
<tr>
<th>Player</th>
<th>#Html.DisplayNameFor(model => model.StartTime)</th>
<th></th>
<th></th>
</tr>
#foreach(var item in Model)
{
<tr>
<td>#Html.DisplayFor(modelItem => item.FullName)</td>
<td>#Html.DisplayFor(modelItem => item.StartTime)</td>
<td>
#Html.ActionLink("Details", "Details", new{id = item.Id})
</td>
<td>
#Html.ActionLink("Edit", "Edit", new {id = item.Id})
</td>
</tr>
}
</table>
</div>
</div>
</div>
</div>
</div>
Sending code:
#Html.ActionLink("Rosters", "Index", "Enrollments", new { id = item.Id }, null) |

MVC 5 - Information in partial view is null after submit

I have a large form and it's becoming too big so I want to start using partial view to keep it cleaner.
Here is a little part of the form that shows the main contact and a list of alternative contacts. The user can add more contacts (the buttons shows a popup to insert new contact) and the user can change the contact from active to inactive (through the checkbox on the table). Right now this is working but like i said i want to be able to use a partial view for the table.
<div class="box box-primary">
<div class="box-header">
<h3 class="box-title">Contacts</h3>
</div>
<div class="box-body">
<div class="form-group">
<div class="col-xs-4">
<label>Phone:</label>
#Html.EditorFor(model => model.Persons.Phone, new { htmlAttributes = new { #class = "form-control", #id = "phone" } })
#Html.ValidationMessageFor(model => model.Persons.Phone, "", new { #class = "text-danger" })
</div>
<div class="col-xs-8">
<label>Email:</label>
#Html.EditorFor(model => model.Persons.Email, new { htmlAttributes = new { #id = "email", #class = "form-control", #placeholder = "Introduza o email..." } })
#Html.ValidationMessageFor(model => model.Persons.Email, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-xs-12">
<label>Address:</label>
#Html.TextAreaFor(model => model.Persons.Address, new { #class = "form-control", #placeholder = "Introduza a morada...", #rows = 3 })
#Html.ValidationMessageFor(model => model.Persons.Address, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-xs-12">
#if (Model.Contacts != null && Model.Contacts.Count > 0)
{
<table id="example2" class="table table-bordered table-hover dataTable" aria-describedby="example2_info">
<thead>
<tr role="row">
<th class="sorting_asc" role="columnheader" tabindex="0" aria-controls="example2" rowspan="1" colspan="1" aria-sort="ascending">Contact</th>
<th class="sorting" role="columnheader" tabindex="0" aria-controls="example2" rowspan="1" colspan="1">Contact Type</th>
<th class="sorting" role="columnheader" tabindex="0" aria-controls="example2" rowspan="1" colspan="1">Active</th>
</thead>
<tbody role="alert" aria-live="polite" aria-relevant="all">
#{
string cssClass = string.Empty;
string activo = string.Empty;
}
#for (var i = 0; i < Model.Contacts.Count; i++)
{
cssClass = i % 2 == 0 ? "even" : "odd";
<tr class="´#cssClass">
<td class=" ">#Html.DisplayFor(model => Model.Contacts[i].Contact)</td>
<td class=" ">#Html.DisplayFor(model => Model.Contacts[i].contacttypes.Name)</td>
<td class=" ">#Html.CheckBoxFor(model => Model.Contacts[i].IsActive, new { #class = "flat-green" })</td>
</tr>
#Html.HiddenFor(model => Model.Contacts[i].ContactsId)
}
</tbody>
</table>
}
</div>
</div>
<div class="form-group">
<div class="col-xs-12">
<input type="button" value="Add New Contact" class="buttonCreate btn btn-primary btn-sm" />
</div>
</div>
</div>
So here is the same HTML using the partial view
<div class="box box-primary">
<div class="box-header">
<h3 class="box-title">Contacts</h3>
</div>
<div class="box-body">
<div class="form-group">
<div class="col-xs-4">
<label>Phone:</label>
#Html.EditorFor(model => model.Persons.Phone, new { htmlAttributes = new { #class = "form-control", #id = "phone" } })
#Html.ValidationMessageFor(model => model.Persons.Phone, "", new { #class = "text-danger" })
</div>
<div class="col-xs-8">
<label>Email:</label>
#Html.EditorFor(model => model.Persons.Email, new { htmlAttributes = new { #id = "email", #class = "form-control", #placeholder = "Introduza o email..." } })
#Html.ValidationMessageFor(model => model.Persons.Email, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-xs-12">
<label>Address:</label>
#Html.TextAreaFor(model => model.Persons.Address, new { #class = "form-control", #placeholder = "Introduza a morada...", #rows = 3 })
#Html.ValidationMessageFor(model => model.Persons.Address, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-xs-12">
#Html.Partial("ContactListControl", Model.Contacts)
</div>
</div>
<div class="form-group">
<div class="col-xs-12">
<input type="button" value="Add New Contact" class="buttonCreate btn btn-primary btn-sm" />
</div>
</div>
</div>
and here is the Partial view:
#model List<RecruitmentWeb.Models.contacts>
<table id="example2" class="table table-bordered table-hover dataTable" aria-describedby="example2_info">
<thead>
<tr role="row">
<th class="sorting_asc" role="columnheader" tabindex="0" aria-controls="example2" rowspan="1" colspan="1" aria-sort="ascending">Contato</th>
<th class="sorting" role="columnheader" tabindex="0" aria-controls="example2" rowspan="1" colspan="1">Tipo de Contato</th>
<th class="sorting" role="columnheader" tabindex="0" aria-controls="example2" rowspan="1" colspan="1">Activo</th>
</thead>
<tbody role="alert" aria-live="polite" aria-relevant="all">
#{
string cssClass = string.Empty;
string activo = string.Empty;
}
#for (var i = 0; i < Model.Count; i++)
{
cssClass = i % 2 == 0 ? "even" : "odd";
<tr class="´#cssClass">
<td class=" ">#Html.DisplayFor(model => Model[i].Contact)</td>
<td class=" ">#Html.DisplayFor(model => Model[i].contacttypes.Name)</td>
<td class=" ">#Html.CheckBoxFor(model => Model[i].IsActive, new { #class = "flat-green" })</td>
</tr>
#Html.HiddenFor(model => Model[i].ContactsId)
}
</tbody>
The Problem
If the user changes a contact from active to inactive or vice-versa, when I submit the form the changes don't go through. In fact, the list is null and contains no information. It works if i don't use partial view.
So what I'm I missing?
You passing only a property of your model to the partial so the the hidden inputs you are generating have the name attribute name="[0].ContactsId", name="[1].ContactsId" etc. whereas they needs to be name="Contacts[0].ContactsId", name="Contacts[1].ContactsId" etc (ditto for the checkboxes).
Change the partial view model to the same as the main view (you haven't indicated what it is), and then pass the model as
#Html.Partial("ContactListControl", Model)
and adjust the Html helpers to suit. However I would recommend you consider using a custom EditorTemplate for RecruitmentWeb.Models.Contacts rather than a partial view for this.

Pass IEnumerable<SomeModel> and SomeModel to View

I'm using:
#model IEnumerable<SomeModel1>
in my view. I cannot change that because multiple controllers in our application are returning some variable with the type:
List<SomeModel1>
I also need to pass the same model (SomeModel1) without the IEnumerable for a modal window:
#model WebApplication8.Models.ManageUserViewModel
Is there a way that I can achieve that without putting the models into a parent model?
UPDATE:
View Page:
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<table class="table table-bordered table-hover" id="tab_logic">
<thead>
<tr>
<th class="text-center">
Active
</th>
<th class="text-center">
Email Address
</th>
<th class="text-center">
First name
</th>
<th class="text-center">
Last Name
</th>
<th class="text-center">
Company
</th>
<th class="text-center">
permissions
</th>
<th class="text-center">
Machine<p>Limit</p>
</th>
<th class="text-center">
Machines<p>Consumed</p>
</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td style="display:none" >#Html.DisplayFor(model => item.UserId)</td>
<td align="center">
<div class="btn-group btn-toggle">
#if (item.ActiveUser)
{
<button class="btn btn-sm btn-success active">ON</button>
<button class="btn btn-sm btn" onclick="DeActivateUser('#item.UserId')">OFF</button>
}
else
{
<button class="btn btn-sm btn" onclick="ActivateUser('#item.UserId')">ON</button>
<button class="btn btn-sm btn-success active">OFF</button>
}
</div>
</td>
<td>#Html.DisplayFor(model => item.EmailAddress)</td>
<td>#Html.DisplayFor(model => item.FirstName)</td>
<td>#Html.DisplayFor(model => item.LastName)</td>
<td>#Html.DisplayFor(model => item.Company)</td>
<td>
#Html.DisplayFor(model => item.UserPermission)
<b class="dropdown-menu"></b>
<ul class="dropdown-menu">
<li><a onclick="ChangePermission('#item.UserId', '3')">View Only</a></li>
<li><a onclick="ChangePermission('#item.UserId', '4')">View - Print</a></li>
<li><a onclick="ChangePermission('#item.UserId', '5')">View - One Print</a></li>
<li><a onclick="ChangePermission('#item.UserId', '6')">Expire by Use - 5 Opens</a></li>
<li><a onclick="ChangePermission('#item.UserId', '7')">Expire After 5 Days</a></li>
</ul>
</td>
<td align="center">#Html.DisplayFor(model => item.MachineLimit)</td>
<td align="center">#Html.DisplayFor(model => item.MachineCount)</td>
</tr>
}
</tbody>
</table>
</div>
</div>
</div>
<section id="addUser">
#using (Html.BeginForm("Manage", "Account", FormMethod.Post, new { #class = "form-horizontal", role = "form" }))
{
#Html.AntiForgeryToken()
<h2>Add Authorized User(s): </h2>
<hr style="height:7pt;" />
<!-- <hr style="height:0pt; visibility:hidden;" /> -->
<button class="btn btn-default btn-success btn-lg" data-target="#modalId" data-toggle="modal" type="button">
<span class="glyphicon glyphicon-plus"></span> ADD NEW USER
</button>
<div class="modal fade" id="modalId" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Add Authorized User(s)</h4>
</div>
<div class="modal-body">
#Html.ValidationSummary()
<!-- <div class="col-md-4"> -->
<div class="form-group">
#Html.LabelFor(m => m. ().FirstName, new { #class = "col-md-2 control-label" })
<div class="col-md-10">
#Html.TextBoxFor(m => m.FirstOrDefault().FirstName, new { #class = "form-control" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(m => m.FirstOrDefault().LastName, new { #class = "col-md-2 control-label" })
<div class="col-md-10">
#Html.TextBoxFor(m => m.FirstOrDefault().LastName, new { #class = "form-control" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(m => m.FirstOrDefault().EmailAddress, new { #class = "col-md-2 control-label" })
<div class="col-md-10">
#Html.TextBoxFor(m => m.FirstOrDefault().EmailAddress, new { #class = "form-control" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(m => m.FirstOrDefault().Company, new { #class = "col-md-2 control-label" })
<div class="col-md-10">
#Html.TextBoxFor(m => m.FirstOrDefault().Company, new { #class = "form-control" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(m => m.FirstOrDefault().UserPermission, new { #class = "col-md-2 control-label" })
<div class="col-md-3 control-label">
#Html.DropDownListFor(m => m.FirstOrDefault().UserPermission, new SelectList(new List<Object>
{
new { value = "2" , text = "View Only"},
new { value = "3" , text = "View - Print" },
new { value = "4" , text = "View - One Print" },
new { value = "5" , text = "Expire by 5 Use" },
new { value = "6" , text = "Expire by 5 Date" }
},
"value", "text", "ViewOnly"))
</div>
#Html.LabelFor(m => m.FirstOrDefault().MachineLimit, new { #class = "col-md-2 control-label" })
<div class="col-md-2 control-label">
#Html.DropDownListFor(m => m.FirstOrDefault().MachineLimit, new SelectList(new List<Object>
{
new { value = "1" , text = "1"},
new { value = "2" , text = "2" },
new { value = "3" , text = "3" },
new { value = "4" , text = "4" },
new { value = "5" , text = "5" }
},
"value", "text", 1))
</div>
</div>
</div>
<div class="modal-footer">
<div class="col-md-3 col-md-10 control-label">
<input type="submit" class="btn btn-default" value="Select/Add" name="Action:Insert" />
</div>
</div>
</div>
</div>
</div>
}
</section>
Passing multiple types into a single view is not really recommended and probably means you are misusing the view in some way. You have 3 options:
Distinct Views
Change what you are doing and make a view for each type.
dynamic Type
Use dynamic as your model type, but this means your view code is littered with if statements - not a good design.
Wrap in a List
Wrap the SomeModel object in a List like this:
SomeModel myModel;
return View(new List<SomeModel> { myModel };
In the case where you have to use SomeModel1,
you can use #Model.firstordefault().{class properties} by keeping IEnumerable declaration above.
Use ViewBag.
// controller
ViewBag.SomeModel = someModel;
// view
#ViewBag.SomeModel.SomeProperty

Resources