How to pass value of radio buttom with Html.ActionLink to controller - asp.net-mvc

How can i pass my radio value with action link to controller
this is my View Page code :
foreach (var item in Model)
{
<div style="height:20px; background-color:#e1e1e1; border-radius:5px; padding:5px">
<div style="float: left; margin-right: 10px">
#Html.RadioButton("DealerRadio", item.id, new { #style = "margin-right:10px" })
</div>
<div style="float: left; margin-right: 30px">
#Html.DisplayFor(modelItem => item.company, new { #style = "margin-right:30px" })
</div>
<div style="float: left; margin-right: 30px">
#Html.DisplayFor(modelItem => item.address, new { #style = "margin-right:10px" })
</div>
<div style="float:right">
#Html.Label("Tell:") #Html.DisplayFor(modelItem => item.contact)
</div>
</div>
<div class="clear"></div>
}
<p>#Html.ActionLink("BUY NOW", "PreOrder", new { Dealer_id = "", Tire_id = ViewBag.Tireid }, new { #class = "btn btn-Buy" })</p>
pleas help me to do this

Related

How to reference a view model property in Asp.Net MVC - Razor view

In my razor view, in the "Panel area - to hold the comment button", I am trying to referencing the view models property BlogPublishedByBlogId.BlogId but I get - "the name BlogPublishedByBlogId does not exist in the current context".
However, I am able to reference the other properties fine using Lamda: model => model.BlogPublishedByBlogId.CreatedDateTime
How do I reference the 'blogId' in the "Panel area - to hold the comment button" area?
#model GbngWebClient.Models.BlogPublishedByBlogIdVM
<h2 class="page-header"><span class="blogtitle">#Session["BlogTitle"]</span></h2>
#{
Layout = "~/Views/Shared/_LayoutUser.cshtml";
}
#if (Model != null)
{
<div class="panel panel-default toppanel">
<div class="panel-body">
<div class="row">
<div class="col-md-2">
#Html.LabelFor(model => model.BlogPublishedByBlogId.CreatedDateTime)
#Html.TextBoxFor(model => model.BlogPublishedByBlogId.CreatedDateTime, new { #class = "form-control", #disabled = "disabled" })
</div>
<div class="col-md-2">
#Html.LabelFor(model => model.BlogPublishedByBlogId.ModifiedDateTime)
#Html.TextBoxFor(model => model.BlogPublishedByBlogId.ModifiedDateTime, new { #class = "form-control", #disabled = "disabled" })
</div>
</div>
<br />
<div class="row">
<div>
#Html.TextAreaFor(model => model.BlogPublishedByBlogId.BlogContent, new { #class = "form-control blogContent", #disabled = "disabled" })
</div>
</div>
</div>
#* Panel area - to hold the comment button. *#
<div class="panel-footer">
<button type="button" class="btn btn-default Comment" data-id="BlogPublishedByBlogId.BlogId" value="Comment">
<span class="glyphicon glyphicon-comment" aria-hidden="true"></span> Get Comment(s)
</button>
</div>
<div id="#string.Format("{0}_{1}","commentsBlock", BlogPublishedByBlogId.BlogId)" style="border: 1px solid #f1eaea; background-color: #eaf2ff;">
<div class="AddCommentArea" style="margin-left: 30%; margin-bottom: 5px; margin-top: 8px;">
<input type="text" id="#string.Format("{0}_{1}", "comment", BlogPublishedByBlogId.BlogId)" class="form-control" placeholder="Add a Comment about the blog..." style="display: inline;" />
<button type="button" class="btn btn-default addComment" data-id="BlogPublishedByBlogId.BlogId"><span class="glyphicon glyphicon-comment" aria-hidden="true"></span></button>
</div>
</div>
</div>
}
The view model:
namespace GbngWebClient.Models
{
public class BlogPublishedByBlogIdVM
{
public BlogPublishedByBlogIdVM()
{
this.BlogPublishedByBlogId = new BlogPublishedByBlogId();
}
public BlogPublishedByBlogId BlogPublishedByBlogId { get; set; }
}
}
I added #Model to the front of:
BlogPublishedByBlogId.BlogId.

Popover bootstrap inside td table

I have this code in an MVC project.
I have a note field inside every row in a simple table
like so:
$(document).ready(function() {
//POPOVER NOTE
$("td").children("#po1").popover({
html: true,
title: 'Note',
content: function() {
return $("td").children(".popoverContent").html();
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<td>
<a href="#" id="po1" class="btn btn-info" data-container="body" data-toggle="popover" data-placement="bottom" data-html="true">
<span class="glyphicon glyphicon-paperclip"></span>
</a>
<div class="popoverContent" style="display: none !important">
<div class="form-group">
#Html.DisplayFor(modelItem => item.Note, new { htmlAttributes = new { #class = "form-control", #readonly = "readonly" } })
</div>
</div>
</td>
The code above returns the content of first row in every case.
What am I wrong ?

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?

Getting error of "The model item passed into the dictionary is of type...."

I'm making a website using asp.net mvc 4 & EF6 where admins can add new client information. So far everything is working fine but whenever I try to save the data by pressing Add button I get this error,
The model item passed into the dictionary is of type 'MyMvc.Models.UserInfo', but this dictionary requires a model item of type 'MyMvc.Models.BrManagement'
Here are my codes,
Controller
[HttpPost]
public ActionResult ClientManager(BrManagement ClTable)
{
if (Session["AdminNAME"] != null)
{
if (ModelState.IsValid)
{
var AddClient = ClTable.AddUserInfo;
abdb.UserInfoes.Add(AddClient);
abdb.SaveChanges();
return RedirectToAction("ClientManager", new { ClPanelId = "AllCl" });
}
return View(ClTable.AddUserInfo);
}
else
{
return RedirectToAction("AdminLogin");
}
}
Model
public class BrManagement
{
public Branch Branches { get; set; }
public IEnumerable<Branch> BrCollection { get; set; }
public UserInfo AddUserInfo { get; set; }
public IEnumerable<UserInfo> UserCollection { get; set; }
}
View
#using (Html.BeginForm("ClientManager", "Home", FormMethod.Post))
{
#Html.ValidationSummary(true)
<div class="editor-label">
<strong>Client USER ID</strong>
</div>
<div class="editor-field">
#Html.TextBoxFor(a => a.AddUserInfo.UserId)
#Html.ValidationMessageFor(a => a.AddUserInfo.UserId)
</div>
<div class="editor-label">
<strong>Client Password</strong>
</div>
<div class="editor-field">
#Html.TextBoxFor(a => a.AddUserInfo.Password)
#Html.ValidationMessageFor(a => a.AddUserInfo.Password)
</div>
<div class="editor-label">
<strong>Full Name</strong>
</div>
<div class="editor-field">
#Html.TextBoxFor(a => a.AddUserInfo.Name, new { size = 30 })
#Html.ValidationMessageFor(a => a.AddUserInfo.Name)
</div>
<div class="editor-label">
<strong>Address</strong>
</div>
<div class="editor-field">
#Html.TextBoxFor(a => a.AddUserInfo.AddressLine1, new { size = 30 })
#Html.ValidationMessageFor(a => a.AddUserInfo.AddressLine1)
</div>
#Html.HiddenFor(a => a.AddUserInfo.CreatedDate, new { #Value = System.DateTime.Now })
#Html.HiddenFor(a => a.AddUserInfo.IsActive, new { #Value = "N" })
#Html.HiddenFor(a => a.AddUserInfo.IsApproved, new { #Value = "N" })
#Html.HiddenFor(a => a.AddUserInfo.IsinfoMatched, new { #Value = "N" })
#Html.HiddenFor(a => a.AddUserInfo.IsReportView, new { #Value = "N" })
#Html.HiddenFor(a => a.AddUserInfo.IsVarified, new { #Value = "N" })
<br />
<p><input type="submit" class="btn btn-info" value="Add" /></p>
}
I've used the same code for Branches & BrCollection model, both of them are working fine. UserCollection model is also working fine. Why is this happening for AddUserInfo model? I've searched a lot but couldn't find any solution similar to mine. Need this help really bad. Your help will be appreciated! Tnx.
UPDATE
View(FULL)
#model ABCoLtd.Models.BrManagement
#{
ViewBag.Title = "ClientManager";
string active = ViewBag.ClActive.ToString();
Layout = "~/Views/Shared/_ALayout.cshtml";
}
<link href="~/Content/DataTables-1.10.4/css/jquery.dataTables.min.css" rel="stylesheet" />
<body>
<script src="~/Scripts/DataTables-1.10.4/jquery.dataTables.min.js"></script>
<script>
$(document).ready(function () {
var tabpane = '#active';
$("#" + tabpane).addClass("active");
$('#BrTable').DataTable({
"aoColumns": [
{ "bSortable": true },
{ "bSortable": true },
{ "bSortable": true },
{ "bSortable": true }
]
});
});
</script>
<br /><br /><br /><br />
<div class="container well" style="min-width: 100%; padding-right: 5px;">
<h3>Client Manager</h3><hr style="border-top: 2px solid #096596;" />
<ul class="nav nav-tabs">
<li>All Clients </li>
<li>Add Clients</li>
</ul>
<div class="tab-content">
<div class="tab-pane" id="AllCl" style="padding-top: 10px; padding-left: 10px;">
<h4>Manage Clients</h4><hr style="border-top: 2px solid #096596;" />
<div class="table-responsive">
<table id="BrTable" class="table table-striped">
<thead>
<tr><th>BOID</th><th>Name</th><th>Email</th><th>Phone</th></tr>
</thead>
<tbody>
#foreach(var item in Model.UserCollection)
{
<tr>
<td>#Html.DisplayFor(modelItem => item.UserId)</td>
<td>#Html.DisplayFor(modelItem => item.Name)</td>
<td>#Html.DisplayFor(modelItem => item.Email)</td>
<td>#Html.DisplayFor(modelItem => item.Phone1)</td></tr>
}
</tbody>
</table>
</div>
</div>
<div class="tab-pane" id="AddCl" style="padding-top: 10px; padding-left: 10px;">
<h4>Add Client</h4><hr style="border-top: 2px solid #096596;" />
#using (Html.BeginForm("ClientManager", "Home", FormMethod.Post))
{
#Html.ValidationSummary(true)
<div class="editor-label">
<strong>Client BO Account No.</strong>
</div>
<div class="editor-field">
#Html.TextBoxFor(a => a.AddUserInfo.BOAccountNo, new { size = 30 })
#Html.ValidationMessageFor(a => a.AddUserInfo.BOAccountNo)
</div>
<div class="editor-label">
<strong>Client USER ID</strong>
</div>
<div class="editor-field">
#Html.TextBoxFor(a => a.AddUserInfo.UserId)
#Html.ValidationMessageFor(a => a.AddUserInfo.UserId)
</div>
<div class="editor-label">
<strong>Client Password</strong>
</div>
<div class="editor-field">
#Html.TextBoxFor(a => a.AddUserInfo.Password)
#Html.ValidationMessageFor(a => a.AddUserInfo.Password)
</div>
<div class="editor-label">
<strong>Full Name</strong>
</div>
<div class="editor-field">
#Html.TextBoxFor(a => a.AddUserInfo.Name, new { size = 30 })
#Html.ValidationMessageFor(a => a.AddUserInfo.Name)
</div>
<div class="editor-label">
<strong>Address</strong>
</div>
<div class="editor-field">
#Html.TextBoxFor(a => a.AddUserInfo.AddressLine1, new { size = 30 })
#Html.ValidationMessageFor(a => a.AddUserInfo.AddressLine1)
</div>
#Html.HiddenFor(a => a.AddUserInfo.CreatedDate, new { #Value = System.DateTime.Now })
#Html.HiddenFor(a => a.AddUserInfo.IsActive, new { #Value = "N" })
#Html.HiddenFor(a => a.AddUserInfo.IsApproved, new { #Value = "N" })
#Html.HiddenFor(a => a.AddUserInfo.IsinfoMatched, new { #Value = "N" })
#Html.HiddenFor(a => a.AddUserInfo.IsReportView, new { #Value = "N" })
#Html.HiddenFor(a => a.AddUserInfo.IsVarified, new { #Value = "N" })
#Html.HiddenFor(a => a.AddUserInfo.UserType, new { #Value = "C" })
<br />
<p><input type="submit" class="btn btn-info" value="Add" /></p>
}
</div>
</div>
</div>
</body>
Your view expect BrManagement
#model MyMvc.Models.BrManagement
But you returned ClTable.AddUserInfo which is type of UserInfo, that is why you got that error.
To fix this, change return View(ClTable.AddUserInfo); to return View(ClTable); in your controller.

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.

Resources