Bootstrap two column layout for MVC create view - asp.net-mvc

I have scaffolded a standard create view for one of my forms using MVC bootstrap. This form however has too many input fields to have in a single column layout, its just too long and seems daft to waste the white space to the right.
I've had a look at this question and tried to get it working with my form with not much luck.
In a nutshell I want the address fields to be on the right, in line with all the other fields.
Razor Snippet
<div class="row">
<div class="col-sm-4">
<div class="form-group">
#Html.LabelFor(model => model.SiteNumber, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.SiteNumber, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.SiteNumber, "", new { #class = "text-danger" })
</div>
</div>
</div>
<div class="col-sm-4">
<div class="form-group">
#Html.LabelFor(model => model.AddressLineOne, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.AddressLineOne, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.AddressLineOne, "", new { #class = "text-danger" })
</div>
</div>
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.SiteName, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.SiteName, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.SiteName, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Department, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Department, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Department, "", new { #class = "text-danger" })
</div>
</div>
So AddressLineOne should be on the same row as SiteNumber, AddressLineTwo should be on the same rowas SiteName and so on and so forth.
This is what I get with the above attempt for the first row:
And this is what I want:
How do I achieve this, whilst keeping the labels and standard spacing to the left.

This is what you want.
.clearfix{
clear: both;
padding: 0;
margin: 0;
height: 5px;
display: block;}
<div class="row">
<div class="col-md-6">
<div class="form-group">
#Html.LabelFor(model => model.SiteNumber, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.SiteNumber, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.SiteNumber, "", new { #class = "text-danger" })
</div>
</div>
<div class="clearfix"></div>
<div class="form-group">
#Html.LabelFor(model => model.SiteName, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.SiteName, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.SiteName, "", new { #class = "text-danger" })
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
#Html.LabelFor(model => model.AddressLineOne, htmlAttributes: new { #class = "control-label col-md-3" })
<div class="col-md-9">
#Html.EditorFor(model => model.AddressLineOne, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.AddressLineOne, "", new { #class = "text-danger" })
</div>
</div>
<div class="clearfix"></div>
<div class="form-group">
#Html.LabelFor(model => model.Department, htmlAttributes: new { #class = "control-label col-md-3" })
<div class="col-md-9">
#Html.EditorFor(model => model.Department, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Department, "", new { #class = "text-danger" })
</div>
</div>
</div>
</div>
</div>

Basically
<div class="row">
<div class="col-md-6"> <!-- Adjust this to be sm/md/lg, whatever fits best for you -->
<div class="form-group">
#Html.LabelFor(model => model.SiteNumber, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.SiteNumber, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.SiteNumber, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.SiteName, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.SiteName, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.SiteName, "", new { #class = "text-danger" })
</div>
</div>
<!-- Continue your first column of form groups here -->
</div>
<div class="col-md-6"> <!-- Adjust this to be sm/md/lg, whatever fits best for you-->
<div class="form-group">
#Html.LabelFor(model => model.AddressLineOne, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.AddressLineOne, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.AddressLineOne, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.AddressLineTwo, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.AddressLineTwo, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.AddressLineTwo, "", new { #class = "text-danger" })
</div>
</div>
<!-- Continue your second column of form groups here -->
</div>
</div>

For me it was enough to do it like this:
<div class="form-group">
#Html.LabelFor(model => model.LOA_ID, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-3">
#Html.EditorFor(model => model.LOA_ID, new { htmlAttributes = new { #class = "form-control", #readonly = "readonly" } })
#Html.ValidationMessageFor(model => model.LOA_ID, "", new { #class = "text-danger" })
</div>
#Html.LabelFor(model => model.LI_ID, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-3">
#Html.EditorFor(model => model.LI_ID, new { htmlAttributes = new { #class = "form-control", #readonly = "readonly" } })
#Html.ValidationMessageFor(model => model.LI_ID, "", new { #class = "text-danger" })
</div>
</div>

Related

How to view edit view inside the modal

I've tried so many ways to load the 'edit' partial view inside my modal but still no luck for it. I'm new to MVC, I've tried to do CRUD operation inside the modal window. Without modal i can perform CRUD operation. Now i want to do CRUD operation inside the modal window.
This my Script section
<script>
$(document).ready(function () {
$(document).on('click', '#edit', function (event) {
event.preventDefault();
event.stopImmediatePropagation();
$.ajax({
url: '/Companies/_Edit',
type: 'GET',
success: function (data) {
$('#createCompanyFormModalbody').html(data);
$('#createCompanyFormModal').modal('show');
},
error: function () {
alert("There is some problem in the service!");
}
});
});
});
</script>
This is my Edit Partial view
#model Mvc.Models.Company
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Company</h4>
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
#Html.HiddenFor(model => model.CompanyID)
<div class="form-group">
#Html.LabelFor(model => model.Name, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Name, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Name, "", 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 class="form-group">
#Html.LabelFor(model => model.PhoneNumber, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.PhoneNumber, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.PhoneNumber, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.URL, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.URL, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.URL, "", new { #class = "text-danger" })
</div>
</div>
<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 class="form-group">
#Html.LabelFor(model => model.Active, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
#Html.EditorFor(model => model.Active)
#Html.ValidationMessageFor(model => model.Active, "", new { #class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Tstamp, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Tstamp, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Tstamp, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.IsDeleted, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
#Html.EditorFor(model => model.IsDeleted)
#Html.ValidationMessageFor(model => model.IsDeleted, "", new { #class = "text-danger" })
</div>
</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>
#Html.ActionLink("Back to List", "Index")
</div>
This is my Controller class
public ActionResult _Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Company company = db.Companies.Find(id);
if (company == null)
{
return HttpNotFound();
}
// return View(company);
return PartialView(company);
}
// POST: Companies/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult _Edit([Bind(Include = "CompanyID,Name,Address,PhoneNumber,URL,Email,Active,Tstamp,IsDeleted")] Company company)
{
if (ModelState.IsValid)
{
db.Entry(company).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(company);
}

How to apply the Bootstrap template to the MVC project

I am changing the style of my MVC project, for that I resorted to Bootswach and its different templates, the problem is that the visual changes in the views are not effective, for example ...
It has a very extensive form vertically, as follows
and you want to align objects to the right and to the left in the following way
to make this change use the tag <div class = "col-md-6">
but I'm not getting the expected result, why does this happen?
now I will tell you how to implement this template ....
1.- download the bootstrap watch bootstrap.css file and add it to my project
2.- in my BundleConfig.cs change the boostrap.css file that you download
3.- And modify my view finally
My View:
<h2>Crear Producto</h2>
#using (Html.BeginForm("Create", "Productoes", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="col-md-6">
<div class="form-group">
#Html.LabelFor(model => model.v_Nombre, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.v_Nombre, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.v_Nombre, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.FotoFile, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
<div class="file-input-new">
<div class="input-group" style="width:280px">
<div tabindex="500" class="form-control file-caption">
<div class="file-caption-name" id="NombreArchivo">
</div>
</div>
<div class="input-group-btn" style="height:auto">
<div tabindex="500" class="btn btn-primary btn-file">
<i class="glyphicon glyphicon-folder-open"></i>
Buscar Foto...
#Html.TextBoxFor(modelo => modelo.FotoFile, new { type = "file", id = "files" })
</div>
</div>
</div>
</div>
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Precio_Minimo, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Precio_Minimo, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Precio_Minimo, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Precio_Maximo, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Precio_Maximo, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Precio_Maximo, "", new { #class = "text-danger" })
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
#Html.LabelFor(model => model.Activo, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
#Html.EditorFor(model => model.Activo)
#Html.ValidationMessageFor(model => model.Activo, "", new { #class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Stock, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Stock, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Stock, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.f_Compra, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.f_Compra, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.f_Compra, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Comentarios, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Comentarios, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Comentarios, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Kn_CodigoCategoria, "Categoria", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownList("Kn_CodigoCategoria", null, htmlAttributes: new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.Kn_CodigoCategoria, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Crear" class="btn btn-outline-success" />
</div>
</div>
</div>
</div>
}
with these two steps I can already visualize the changes of the template, but I said previously I can not move my objects as far as I want,
what am I doing wrong? Do I have to add other references to my BundleConfig? do I have to install something in the project?
any help for me?
You need to put the col-x-y divs inside a div with class .row and inside a container or it will not work. Similar to this (you do not need the col class but I just put it here so it is easier to see the columns in the fiddle):
<div class="container">
<!--This will not work -->
<div class="col col-sm-3">.col-sm-3</div>
<div class="col col-sm-6">.col-sm-6</div>
<div class="col col-sm-3">.col-sm-3</div>
<!-- This will work -->
<div class="row">
<div class="col col-sm-3">.col-sm-3</div>
<div class="col col-sm-6">.col-sm-6</div>
<div class="col col-sm-3">.col-sm-3</div>
</div>
</div>
Here is the Fiddle so you can see this yourself.

Client validation in Partial View submiting

When Partial View load in _Layout directly, I have no Problem in Client Validation and shows field by filed error, But When call Partial View as same as one part of index page:
#Html.Partial("_CreateUser", Model)
_CreateUser PartialView:
#model ProjInMvc.Models.User
<div id="update_panel"></div>
#using (Ajax.BeginForm("CreateUser", "Home",null, new AjaxOptions { UpdateTargetId = "update_panel", Url = "/Home/CreateUser", HttpMethod = "Post",InsertionMode =InsertionMode.Replace }, new { #class = "pull-right" }))
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(model => model.FirstName, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.FirstName, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.LastName, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.LastName, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.LastName, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.UserName, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.UserName, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.UserName, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default")" />
</div>
</div>
</div>
}
Can anyone help me to understand this problem?
Tnx.
You may be call in Html.BeginForm, form in form is incorrect, please check that

While saving an image in my MVC WebApp, ModelState.IsValid always shows false

During Create, i always get the ModelState.IsValid as false and dont save my image in the database
In my model, the field for saving images is
[Required]
public byte[] BookingImage { get; set; }
In my controller, I have the following code:
public ActionResult Create([Bind(Include = "BookingId,BookingName,BookingDescription,ProviderId,EmployeeId,StatusId,BookingDuration,BookingTime, BookingImage, BookingRequestAmount,BookingReserveDeadline")] Booking booking, HttpPostedFileBase BookingImageHidden)
{
using (var ms = new MemoryStream())
{
BookingImageHidden.InputStream.CopyTo(ms);
booking.BookingImage = ms.ToArray();
}
if (ModelState.IsValid)
{
db.Booking.Add(booking);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.EmployeeId = new SelectList(db.Employee, "EmployeeId", "EmployeeName", booking.EmployeeId);
ViewBag.ProviderId = new SelectList(db.Providers, "ProviderId", "ProviderName", booking.ProviderId);
ViewBag.StatusId = new SelectList(db.Status, "StatusId", "StatusName", booking.StatusId);
return View(booking);
}
In my View, this is the code around the Image:
#model Sword.Models.Booking
#using (Html.BeginForm("Create", "bookings", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Booking</h4>
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(model => model.BookingName, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.BookingName, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.BookingName, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.BookingDescription, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.BookingDescription, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.BookingDescription, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.ProviderId, "ProviderId", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownList("ProviderId", null, htmlAttributes: new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.ProviderId, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.EmployeeId, "EmployeeId", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownList("EmployeeId", null, htmlAttributes: new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.EmployeeId, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.StatusId, "StatusId", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownList("StatusId", null, htmlAttributes: new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.StatusId, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.BookingDuration, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.BookingDuration, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.BookingDuration, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.BookingTime, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.BookingTime, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.BookingTime, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.BookingImage, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.BookingImage, new { htmlAttributes = new { #class = "form-control" } })
<input name="BookingImageHidden" type="file" />
#Html.ValidationMessageFor(model => model.BookingImage, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.BookingRequestAmount, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.BookingRequestAmount, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.BookingRequestAmount, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.BookingReserveDeadline, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.BookingReserveDeadline, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.BookingReserveDeadline, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>
In my model, the field for saving images is [Required] public byte[]
BookingImage { get; set; }
Why do you even have a property in your model for the image if you are going to manually be reading it.
So simply consider using view models with the proper types (please notice that I am bolding the word view model here):
public class BookingViewModel
{
[Required]
public HttpPostedFileBase BookingImage { get; set; }
... some other properties that you need populated from the client
}
and then in your controller action get rid of these hacks and use the view model:
public ActionResult Create(BookingViewModel bookingVm)
{
if (ModelState.IsValid)
{
// In the MapFromViewModel you will obviously only
// map the properties that you need to be populated
// in your domain model from the view model. That's where
// you should put the logic of reading the image stream into a
// byte array which is what your domain model expects.
Booking booking = MapFromViewModel(bookingVm);
db.Booking.Add(booking);
db.SaveChanges();
return RedirectToAction("Index");
}
...
}
You got into all those troubles because you didn't use view models in the first place but rather you had your controller actions take your domain models as parameter which is one of the worst MVC design patterns I have ever seen in my life.

Asp net mvc submit button not doing anything

I'm trying to save an operation (Virement/Transfer, deposit, payment, etc). Operation Entity has a lot of properties. For every operation like transfer I have to choose only the matching properties for transfer, but the submit action doesn't do any thing. What is wrong?
OpérationController:
public ActionResult CreateVS()
{
PopulateClientsDropDownList();
PopulateUtilisateursDropDownList();
PopulateComptesDropDownList();
PopulateTypeOpérationsDropDownList();
PopulateCatégorieOpérationsDropDownList();
PopulateDevisesDropDownList();
PopulateMvtCaissesDropDownList();
PopulateModePaiementsDropDownList();
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CreateVS(
[Bind(Include = "OpérationId,TypeOpérationId,CatégorieOpération,RIBCrediteur,RIBDébiteur,IdClient,CompteId,CléRIB,NomExpéditeur,NomBeneficiare,Date,EtatValidation,Référence,Montant,Signe,Agent,DeviseId,ModePaiementId")]
Opération a)
{
try
{
if (ModelState.IsValid)
{
service.CreateOpération(a);
service.SaveOpération();
return RedirectToAction("VSEffectué");
// return Content("virement réussi");
}
}
catch (RetryLimitExceededException)
{
ModelState.AddModelError("", "Incapable d'enregistrer les modifications.Contacter l'Admin si le probléme persist.");
}
PopulateClientsDropDownList(a.OpérationId);
PopulateComptesDropDownList(a.CompteId);
PopulateTypeOpérationsDropDownList(a.TypeOpérationId);
PopulateCatégorieOpérationsDropDownList(a.CatégorieOpérationId);
PopulateDevisesDropDownList(a.DeviseId);
PopulateMvtCaissesDropDownList(a.MvtCaisseId);
PopulateModePaiementsDropDownList(a.ModePaiementId);
return View(a);
}
View:
#model BankExpress.Domain.Entities.Opération
#{ViewBag.Title = "CreateVS";}
#using (Html.BeginForm(""CreateVS", "Opération""))
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<hr /><br />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(model => model.TypeOpérationId, "Type Opération", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-4">
#Html.DropDownList("TypeOpérationId", null, "Saisi Type Opération", htmlAttributes: new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.TypeOpérationId, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.CatégorieOpérationId, "Catégorie Opération", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-4">
#Html.DropDownList("CatégorieOpérationId", null, "Saisi Catégorie Opération", htmlAttributes: new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.CatégorieOpérationId, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.RIBCrediteur, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-4">
#Html.EditorFor(model => model.RIBCrediteur, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.RIBCrediteur, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.RIBDébiteur, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-4">
#Html.EditorFor(model => model.RIBDébiteur, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.RIBDébiteur, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.IdClient, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-4">
#Html.EditorFor(model => model.IdClient, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.IdClient, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.CompteId, "N°Compte", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-4">
#Html.DropDownList("CompteId", null, "Saisi N°Compte", htmlAttributes: new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.CompteId, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.CléRIB, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-4">
#Html.EditorFor(model => model.CléRIB, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.CléRIB, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.NomExpéditeur, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-4">
#Html.EditorFor(model => model.NomExpéditeur, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.NomExpéditeur, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.NomBeneficiare, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-4">
#Html.EditorFor(model => model.NomBeneficiare, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.NomBeneficiare, "", 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-4">
#Html.EditorFor(model => model.Date, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Date, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.EtatValidation, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-4">
#Html.EnumDropDownListFor(model => model.EtatValidation, htmlAttributes: new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.EtatValidation, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Montant, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-4">
#Html.EditorFor(model => model.Montant, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Montant, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Signe, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-4">
#Html.EditorFor(model => model.Signe, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Signe, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Agent, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-4">
#Html.EditorFor(model => model.Agent, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Agent, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.DeviseId, "Devise", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-4">
#Html.DropDownList("DeviseId", null, "Saisi Devise", htmlAttributes: new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.DeviseId, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.ModePaiementId, "Mode Paiement", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-4">
#Html.DropDownList("ModePaiementId", null, "Saisi Mode Paiement", htmlAttributes: new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.ModePaiementId, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-3">
<input type="submit" value="Valider" name="CreateVS" class="btn btn-primary" />
</div>
#Html.ActionLink("Annuler", "Index", "Opération", new { #class = "col-md-pull-2 btn btn-primary" })
</div>
</div>
}
Remove the extra quotes in the beginform:
#using (Html.BeginForm(""CreateVS", "Opération""))
Should be
#using (Html.BeginForm("CreateVS", "Opération"))
Try this
#using (Html.BeginForm("AnswerView", "DarulIftah", FormMethod.Post))

Resources