MVC controller giving me strange situation when passing model to controller - c#

Below is picture where during debug you can see controller receiving correct id but image = null,which is fine because as can see my htm.beginform() is not handling the file request and my code works except the image not saved to db
in this second image below when I add code into htmbeginform() so it handles the file upload (by the way I used same code when creating new advert and it worked) for some reason the model returns id=0 although it has image data now,but because id=0 the advert doesn’t get updated coz that’s not the advert id
I have tried using linq and had same trouble im not understanding why keep getting id=0 when rest of model data is fine
I am using a repository pattern hence my business logic is in another layer and use maodel to pass data but dont think thats an issue here
#model Template.Model.AdvertModel
<h1>Update a tender Advert View</h1>
<br />
#using (Html.BeginForm("UpdateAdvert", "Manager", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<fieldset>
<legend>Advert details</legend>
<div class="row">
#Html.LabelFor(model => model.TenderId, "Tender Ref Number:", new { #class = "control-label col-md-3" })
#Html.EditorFor(model => model.TenderId)
#Html.ValidationMessageFor(model => model.TenderId, null, new { #class = "text-danger" })
</div><br />
<div class="row">
#Html.LabelFor(model => model.TenderTitle, "Title:", new { #class = "control-label col-md-3" })
#Html.TextAreaFor(model => model.TenderTitle, 5, 55, null)
#Html.ValidationMessageFor(model => model.TenderTitle, null, new { #class = "text-danger" })
</div><br />
<div class="row">
#Html.LabelFor(model => model.Description, "Desription:", new { #class = "control-label col-md-3" })
#Html.TextAreaFor(model => model.Description, 10, 55, null)
</div><br />
#*<div class="row">
#Html.LabelFor(model => model.TenderSector, "Tender Sector:", new { #class = "control-label col-md-3" })
#Html.EditorFor(model => model.TenderSector)
</div><br/>*#
<div class="row">
#Html.LabelFor(model => model.TenderSector, new { #class = "control-label col-md-3" })
#Html.DropDownListFor(m => m.TenderSector, new List<SelectListItem>
{
new SelectListItem {Text = "Roads", Value = "Roads"},
new SelectListItem {Text = "Building Construction", Value = "Building Construction"},
new SelectListItem {Text = "Maintainance", Value = "Maintainance"},
new SelectListItem {Text = "Bridges", Value = "Bridges"},
new SelectListItem {Text = "Other", Value = "Other"}
}, "--Select Option--", new { #class = "control-label col-md-2" })
#Html.ValidationMessageFor(m => m.TenderSector, "", new { #class = "text-danger" })
</div>
<br />
<div class="row">
#Html.LabelFor(model => model.TenderIssuer, "Tender Issuer:", new { #class = "control-label col-md-3" })
#Html.EditorFor(model => model.TenderIssuer)
#Html.ValidationMessageFor(model => model.TenderIssuer, null, new { #class = "text-danger" })
</div><br />
<div class="row">
#Html.LabelFor(model => model.TenderMeetingDateTime, "Tender Meeting date and time:", new { #class = "control-label col-md-3" })
#Html.EditorFor(model => model.TenderMeetingDateTime, new { htmlAttributes = new { #class = "date form_datetime" } })
</div><br />
<div class="row">
#Html.LabelFor(model => model.TenderMeetingVenue, "Meeting Venue:", new { #class = "control-label col-md-3" })
#Html.TextAreaFor(model => model.TenderMeetingVenue, 5, 55, null)
</div><br />
<div class="row">
#Html.LabelFor(model => model.CompulsoryMeeting, "Compulsory To Attend Meting:", new { #class = "control-label col-md-3" })
#Html.EditorFor(model => model.CompulsoryMeeting)
</div><br />
<div class="row">
#Html.LabelFor(model => model.TenderContactDetails, "Tender Contact Details:", new { #class = "control-label col-md-3" })
#Html.TextAreaFor(model => model.TenderContactDetails, 5, 55, null)
</div><br />
<div class="row">
#Html.LabelFor(model => model.DocPickUp, "Document Pickup Location:", new { #class = "control-label col-md-3" })
#Html.TextAreaFor(model => model.DocPickUp, 5, 55, null)
#Html.ValidationMessageFor(model => model.DocPickUp, null, new { #class = "text-danger" })
</div><br />
<div class="row">
#Html.LabelFor(model => model.DocDropOff, "Document Drop Off Location:", new { #class = "control-label col-md-3" })
#Html.TextAreaFor(model => model.DocDropOff, 5, 55, null)
#Html.ValidationMessageFor(model => model.DocDropOff, null, new { #class = "text-danger" })
</div><br />
<div class="row">
#Html.LabelFor(model => model.DocCost, "Document Cost: R", new { #class = "control-label col-md-3" })
#Html.EditorFor(model => model.DocCost)
</div><br />
<div class="row">
#Html.LabelFor(model => model.TenderCloseDate, "Date Submission closes:", new { #class = "control-label col-md-3" })
#Html.EditorFor(model => model.TenderCloseDate, new { htmlAttributes = new { #class = "date form_datetime" } })
#Html.ValidationMessageFor(model => model.TenderCloseDate, null, new { #class = "text-danger" })
</div><br />
<div class="row">
#Html.LabelFor(model => model.OtherInfo, "Other Information:", new { #class = "control-label col-md-3" })
#Html.TextAreaFor(model => model.OtherInfo, 10, 55, null)
</div><br />
<div class="row">
#Html.LabelFor(model => model.Active, "Activate Advert (Visible to site visitors):", new { #class = "control-label col-md-3" })
#Html.EditorFor(model => model.Active)
</div><br/>
<div class="row"></div><br/>
#if (Model.Image != null)
{
<div class="row">
<img src="data:image;base64,#System.Convert.ToBase64String(Model.Image)" max-width="100%" max-height="100%"/>
<br/>
#Html.LabelFor(model => model.Image, "Change Image", new {#class = "control-label col-md-3"})
<div class="editor-field">
<input type="file" name="file" id="file"/>
</div>
</div><br/>
}
#if (Model.Image == null)
{
<div class="editor-label">
Image
</div>
<div class="editor-field">
<input type="file" name="file" id="file"/>
</div>
}
#*<div class="editor-label">
Image
</div>
<div class="editor-field">
<input type="file" name="file" id="file" />
</div>*#
<hr />
#*<input type="submit" style="margin-left: 50%" class="btn btn-sky text-uppercase btn-lg ladda-button" value="Create new advert" data-style="expand-right" />*#
<button type="submit" style="margin-left: 50%" class="btn btn-sky text-uppercase btn-lg ladda-button" data-style="expand-right">
<span class="ladda-label"> Update Advert </span>
</button>
</fieldset>
}
<br />

Add a hidden field within BeginForm() for your model.Id
#Html.HiddenFor(model=> model.Id)

Related

fitting iframe in columns

I have 2 columns, the column on the left containing a form and the column on the right containing an iframe which shows a PDF file. I need the PDF as close to the left column as possible in order to fit them both on the page, although no matter what column sizes I use, the left column seems to push the right column out. Can anyone give me a solution to getting both columns side by side?
the edit view:
<div class="container">
<div class="row">
<div class="col-md-10" >
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>SecondarySchoolSurvey</h4>
<br />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
#Html.HiddenFor(model => model.Id)
<div class="form-group">
#Html.LabelFor(model => model.Q1, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Q1, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Q1, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.RollNumber, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.RollNumber, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.RollNumber, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.OfficialSchoolName, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.OfficialSchoolName, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.OfficialSchoolName, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.CampDate, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.CampDate, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.CampDate, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.SurveyFileName, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.SurveyFileName, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.SurveyFileName, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.FilePage, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.FilePage, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.FilePage, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Q2, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EnumDropDownListFor(model => model.Q2, htmlAttributes: new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.Q2, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Q3, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Q3, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Q3, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Q4, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Q4, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Q4, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Q5, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Q5, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Q5, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Q6a, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EnumDropDownListFor(model => model.Q6a, htmlAttributes: new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.Q6a, "", new { #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>
}
</div>
<div class="col-md-2" >
<br />
<br />
<br />
<iframe width="700" height="800" src="#Url.Action("OpenPDF", new { id = 8 })"></iframe>
</div>
</div>
</div>
this is what they look like currently(if i make the left column size any less than 10 then the Labels start to shrink):
When I try both columns as col-md-6 this is what I get (if i make the left column size any less than 10 then the Labels start to shrink as you can see). I am not sure what is causing the white space in between the 2 columns:
You need to separate the label in different column. Give label the enough space and reduced the first column width in two main columns (e.g 6 for form and 6 for pdf container). e.g
<div class="form-group">
<div class="row">
<div class="col-md-6" >
#Html.LabelFor(model => model.Q1, htmlAttributes: new { #class = "control-label col-md-2" })
</div>
<div class="col-md-6">
#Html.EditorFor(model => model.Q1, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Q1, "", new { #class = "text-danger" })
</div>
</div>
</div>

ASP.Net MVC Viewmodel goes null to controller from View

I want to export my Viewmodel from my view to my controller which is receiving it in a method, this is my view:
#using Resources
#model MyProject.ViewModel.CertificationViewModel
#{
ViewBag.Title = Resources.titleCertificationList;
#section css
{
<link href="#Url.Content("~/layout/css/stylesformador.css")" rel="stylesheet" />
<link href="~/Content/themes/base/jquery-ui.css" rel="stylesheet" />
}
}
<div class="mainblock">
<div class="block-group">
<div class="ttitle title">#HIQResources.titleEdit</div>
</div>
#using (Html.BeginForm("Edit", "Certification", FormMethod.Post, new { id = "form1" }))
{
#Html.AntiForgeryToken()
<div class="md-content">
<div class="form-horizontal">
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
#Html.HiddenFor(model => model.Id)
<div class="form-group">
#Html.LabelFor(model => model.pdf, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-8">
#*<input type="file" name="file" />*#
#Html.EditorFor(model => model.Name, new { htmlAttributes = new { #class = "form-control inputForm", #type="file" } })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Code, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-8">
#Html.EditorFor(model => model.Code, new { htmlAttributes = new { #class = "form-control inputForm", #readonly = "readonly" } })
#Html.ValidationMessageFor(model => model.Code, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.SelectedEntityId, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-8">
#Html.DropDownListFor(model => model.SelectedEntityId, Model.EntitiesDrop, htmlAttributes: new { #class = "form-control inputForm" })
#Html.HiddenFor(model => model.SelectedEntityId)
#Html.ValidationMessageFor(model => model.SelectedEntityId, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.StatusId, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-8">
#Html.DropDownListFor(model => model.StatusId, Model.StatusDrop, htmlAttributes: new { #class = "form-control inputForm" })
#Html.HiddenFor(model => model.StatusId)
#Html.ValidationMessageFor(model => model.StatusId, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Result, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-8">
#Html.EditorFor(model => model.Result, new { htmlAttributes = new { #class = "form-control inputForm" } })
#Html.ValidationMessageFor(model => model.Result, "", new { #class = "text-danger"})
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Date, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-8">
#Html.EditorFor(model => model.Date, new { htmlAttributes = new { #class = "form-control inputForm" } })
#Html.ValidationMessageFor(model => model.Date, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Observation, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-8">
#Html.EditorFor(model => model.Observation, new { htmlAttributes = new { #class = "form-control inputForm" } })
#Html.ValidationMessageFor(model => model.Observation, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-1">
<div class="col-md-8">
<table class="table table-bordered">
<tr>
<th class="col-md-2">
#Html.DisplayNameFor(model => model.FileName)
</th>
<th class="col-md-2">Ficheiro</th>
</tr>
#foreach (var item in Model.teste)
{
<tr>
<td>
#if (item.FileName == null)
{
<h5>Não existem ficheiros para esta certificação.</h5>
}
else
{
#Html.DisplayFor(modelItem => item.FileName)
}
</td>
<td>
#if (item.FileName == null)
{
<h5>Não existem ficheiros para esta certificação.</h5>
}
else
{
Download
#*Delete*#
<input type="submit" value="Edit" id="Delete" class="btn btn-default btn-sm btn-detail" />
}
#*#Html.ActionLink("Download", "Certification", "Download", new { id = Model.Id })*#
</td>
</tr>
}
</table>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-8">
<input type="submit" value="#HIQResources.buttonSave" class="btn btn-default btn-sm btn-detail" />
#Html.ActionLink(HIQResources.buttonBack, "CertificationList", new { id = Model.StudentId, nome = Model.StudentName }, new { #class = "btn btn-default btn-sm btn-edit" })
</div>
</div>
</div>
</div>
}
</div>
#section Scripts {
#Scripts.Render("~/bundles/jquery")
#Scripts.Render("~/bundles/jqueryval")
<script src="~/Scripts/HIQTrainingScripts/Certification/Edit.js"></script>
<script>
$("#Date").datepicker({ dateFormat: 'yy/mm/dd' });
$("#Delete").click(function () {
var vm = $('#Model').serialize();
alert(vm)
var token = $('input[name="__RequestVerificationToken"]').val();
$.ajax({
url: "/Certification/Edit",
type: "POST",
data: {
__RequestVerificationToken: token,
vm: vm,
click: "deu",
},
sucess: function (result) {
alert("sucess");
}
});
});
</script>
}
This is the method in my controller:
[Authorize(Roles = "Admin, Staff")]
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(CertificationViewModel vm, string click) {
if (click == "deu")
{
int lol = _servicesFactory.GetService<ICertificationManager>().DeleteFile(vm, GetLoggedUser());
return View(vm);
}
}
The value of my ViewModel comes always as null to my Jquery variable and I don't know why, because it should come with the info from the view to the controller in order for me, to execute my task.
Am I missing something ?

Submit button not working! MVC

#model CommonLayer.ORDER
#{
ViewBag.Title = "Update";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h4>Update Order Status</h4>
<br />
#if (Model.OrderStatus != "Shipped")
{
using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
#Html.ValidationSummary(true)
#Html.HiddenFor(model => model.UserId)
<div class="form-group">
#Html.LabelFor(model => model.OrderId, htmlAttributes: new { #style = "font-size:medium", #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.OrderId, new { htmlAttributes = new { #class = "form-control", #readonly = "readonly" } })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.OrderStatus, htmlAttributes: new { #style = "font-size:medium", #class = "control-label col-md-2" })
<div class="col-md-10">
<select class="form-control" name="OrderStatus">
<option value="Pending" id="pending">Pending</option>
<option value="Shipped" id="shipped">Shipped</option>
</select>
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.OrderDate, htmlAttributes: new { #style = "font-size:medium", #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.OrderDate, new { htmlAttributes = new { #class = "form-control", #readonly = "readonly" } })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.OrderNumber, htmlAttributes: new { #style = "font-size:medium", #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.OrderNumber, new { htmlAttributes = new { #class = "form-control", #readonly = "readonly" } })
</div>
</div>
<form action="/order/update/#Model.OrderId" method="post" enctype="multipart/form-data">
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Update" class="btn btn-primary" />
</div>
</div>
</form>
</div>
}
}
else
{
<h4>Order Has Been Shipped!</h4>
}
My submit button isn't working regardless of the correct syntax being used. When I press the button nothing happens, as if I haven't clicked it, no redirecting whatsoever.
Was working until yesterday. Did not change any code whatsoever regarding the form or corresponding controller.
This is the controller corresponding to the form.
[HttpGet]
[Authorize(Roles = "ADM")]
public ActionResult Update(Guid Id)
{
BusinessLayer.Orders order = new BusinessLayer.Orders();
return View(order.GetOrder(Id));
}
[HttpPost]
[Authorize(Roles = "ADM")]
public ActionResult Update(CommonLayer.ORDER order, Guid id)
{
BusinessLayer.Orders blorder = new BusinessLayer.Orders();
blorder.UpdateOrder(order);
return RedirectToAction("UpdateDetails", new {id=id});
}
Use below code, let us know if doesn't work
Added below line to pass the orderid
#Html.HiddenFor(model => model.OrderId)
Updated below line to call action of a controller
Html.BeginForm("update","order",FormMethod.Post)
Finally removed the unnecessary form tag
Removed antiforgerytoken from view as you are not checking in you action method.
If you want to add that then you need to add [ValidateAntiForgeryToken] after [httppost] of Update Action method
#model CommonLayer.ORDER
#{
ViewBag.Title = "Update";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h4>Update Order Status</h4>
<br />
#if (Model.OrderStatus != "Shipped")
{
using (Html.BeginForm("Update","Order",FormMethod.Post))
{
<div class="form-horizontal">
#Html.ValidationSummary(true)
#Html.HiddenFor(model => model.UserId)
#Html.HiddenFor(model => model.OrderId)
<div class="form-group">
#Html.LabelFor(model => model.OrderId, htmlAttributes: new { #style = "font-size:medium", #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.OrderId, new { htmlAttributes = new { #class = "form-control", #readonly = "readonly" } })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.OrderStatus, htmlAttributes: new { #style = "font-size:medium", #class = "control-label col-md-2" })
<div class="col-md-10">
<select class="form-control" name="OrderStatus">
<option value="Pending" id="pending">Pending</option>
<option value="Shipped" id="shipped">Shipped</option>
</select>
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.OrderDate, htmlAttributes: new { #style = "font-size:medium", #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.OrderDate, new { htmlAttributes = new { #class = "form-control", #readonly = "readonly" } })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.OrderNumber, htmlAttributes: new { #style = "font-size:medium", #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.OrderNumber, new { htmlAttributes = new { #class = "form-control", #readonly = "readonly" } })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Update" class="btn btn-primary" />
</div>
</div>
</div>
}
}
else
{
<h4>Order Has Been Shipped!</h4>
}

how do get input values from a bootstrap modal popoup in mvc 5 method

Please how do i get input values from a bootstrap modal popup from mvc 5 method, i am calling the method from a button click onclick event using this:
<button type="button" class="btn btn-blue" onclick="location.href = '#Url.Action("SaveToDb")';return false;">
public void SaveToDb(AddressViewModel addrs)
{
var addressLine1 = addrs.AddressLine1;
var addressLine2 = addrs.AddressLine2;
var city = addrs.City;
}
My Form:
<form role="form" class="form-horizontal" method="POST">
<!-- start: BOOTSTRAP EXTENDED MODALS -->
<div id="responsive" class="modal fade" tabindex="-1" data-width="760" style="display: none; top: 50px;">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
×
</button>
<h4 class="modal-title">Add Address</h4>
</div>
<div class="modal-body">
<div class="form-group">
#Html.LabelFor(model => model.ClientId, htmlAttributes : new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.ClientId, new { htmlAttributes = new { #class = "form-control", #ReadOnly = true } })
#Html.ValidationMessageFor(model => model.ClientId, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.AddressLine1, htmlAttributes : new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.AddressLine1, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.AddressLine1, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.AddressLine2, htmlAttributes : new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.AddressLine2, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.AddressLine2, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.City, htmlAttributes : new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.City, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.City, "", new { #class = "text-danger" })
</div>
</div>
<div class="modal-footer">
<button type="button" data-dismiss="modal" class="btn btn-light-grey">
Close
</button>
<button type="button" class="btn btn-blue" onclick="location.href = '#Url.Action("Savetodb")';return false;">
Save changes
</button>
</div>
</div>
</div>
<!-- ends: BOOTSTRAP EXTENDED MODALS -->
i have a break point at SaveToDb method, but AddressViewModel object (addrs) is alway null. please any assistance will be appreciated.
The reason behind this is that your actual form is not posted, you are just redirecting here that's why your form data is not model binding properly. You could do a POST ajax call instead and pass your form data.

MVC5 - View does not return the Id of my object on post

My view is shown below:
#model KtembRegistry.Models.Entities.Member
#{
ViewBag.Title = "Üye Güncelleme";
}
<h2>#ViewBag.Title</h2>
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
#Html.HiddenFor(model => model.MemberId)
#Html.HiddenFor(model => model.Director.EmployeeId);
<div class="form-horizontal">
<hr />
<div class="form-group">
<input type="button" value="Back to List" onclick="location.href='#Url.Action("Index", "Members")'" class="btn btn-primary" />
<input type="submit" value="Save" class="btn btn-success" />
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
#Html.LabelFor(model => model.MemberName, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.MemberName, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.MemberName, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Location, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Location, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Location, "", new { #class = "text-danger" })
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
#Html.LabelFor(model => model.MemberClass, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.MemberClass, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.MemberClass, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.ClassId, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.ClassId, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.ClassId, "", new { #class = "text-danger" })
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
#Html.LabelFor(model => model.LastApprovalDate, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.LastApprovalDate, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.LastApprovalDate, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.TaxNo, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.TaxNo, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.TaxNo, "", new { #class = "text-danger" })
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
#Html.LabelFor(model => model.Phone, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Phone, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Phone, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Fax, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Fax, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Fax, "", new { #class = "text-danger" })
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
#Html.LabelFor(model => model.Mobile, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Mobile, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Mobile, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Address, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Address, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Address, "", new { #class = "text-danger" })
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
#Html.LabelFor(model => model.Email, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Email, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Email, "", new { #class = "text-danger" })
</div>
</div>
</div>
</div>
<br />
<h4>Direktör</h4>
<hr />
<div class="row">
<div class="col-md-6">
<div class="form-group">
#Html.LabelFor(model => model.Director.FirstName, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Director.FirstName, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Director.FirstName, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Director.LastName, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Director.LastName, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Director.LastName, "", new { #class = "text-danger" })
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
#Html.LabelFor(model => model.Director.IdentityNo, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Director.IdentityNo, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Director.IdentityNo, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Director.Responsibility, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Director.Responsibility, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Director.Responsibility, "", new { #class = "text-danger" })
</div>
</div>
</div>
</div>
<br />
<h3> Personel </h3>
<hr />
<table class="table table-hover">
<thead>
<tr>
<th>İsim</th>
<th>Kimlik No</th>
<th>Yetki</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model.Employees)
{
<tr>
<td>#item.FirstName</td>
<td>#item.IdentityNo</td>
<td>#item.Responsibility</td>
<td>
#Html.ActionLink("Edit", "Edit", new { #item.EmployeeId }) |
#Html.ActionLink("Delete", "Delete", new { #item.EmployeeId })
</td>
</tr>
}
</tbody>
</table>
</div>
}
#section Scripts {
#Scripts.Render("~/bundles/jqueryval")
}
Entity:
public class Employee
{
public virtual int Id { get; protected set; }
public virtual int EmployeeNo { get; set; }
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual int IdentityNo { get; set; }
public virtual string Responsibility { get; set; }
public virtual Member Company { get; set; }
}
When I do post, everything is sent to controller just fine except model.Director.EmployeeId. The controller sends the EmployeeId field correctly to the field. Whatever, I do it does not work. Any ideas why my post does not work ?
I have an old habit of making the setter of identity fields in my entities protected in order to protect outside mangling. However, in this case it was causing the identity field from being updated which was the cause of the problem.

Categories