Can't create Model by View and Partialview updated using ajax - asp.net

First of all sorry for my English.
My problem:
I've a table called Inspeccio and a related table called Criteris. The relation is ManyToMany so I've the table CriterisInspeccio.
I've a Model for these tables with the same name.
When I want to create a Inspeccio there's a Dropdownlist for different types of Inspeccio.
When I change the value of this Dropdownlist a list of Criteris is updated using ajax.
But when I submit the form the controller receive the InspeccioViewModel without the Criteris of the list.
The code:
InspecciosController:
// GET: Inspeccios/Create
public ActionResult Create()
{
ViewBag.IdTipusInspeccio = new SelectList(db.TipusInspeccio, "IdTipusInspeccio", "Tipus");
InspeccioViewModel inspeccioViewModel = new InspeccioViewModel();
return View(inspeccioViewModel);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(InspeccioViewModel inspeccioViewModel)
{
if (ModelState.IsValid)
{
Inspeccio inspeccio = InspeccioHelper.ToInspeccio(inspeccioViewModel);
db.Inspeccio.Add(inspeccio);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(inspeccioViewModel);
}
public ActionResult CriterisSegonsTipusInspeccio(int idTipusInspeccio)
{
ICollection<CriterisInspeccioAssignatsViewModel> criteris = CriterisSegonsTipusInspeccioFalse(idTipusInspeccio);
return PartialView("CriterisInspeccioAssignatsViewModel", criteris);
}
Create view:
#model Inspeccions.ViewModels.InspeccioViewModel
#{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
}
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Inspeccio</h4>
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(model => model.Clau, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Clau, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Clau, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.DataCaducitat, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.DataCaducitat, new { htmlAttributes = new { #class = "form-control date-picker" } })
#Html.ValidationMessageFor(model => model.DataCaducitat, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Data, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Data, new { htmlAttributes = new { #class = "form-control date-picker" } })
#Html.ValidationMessageFor(model => model.Data, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Hora, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Hora, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Hora, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Expedient, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Expedient, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Expedient, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.IdTipusInspeccio, "Tipus", htmlAttributes: new {#class = "control-label col-md-2"})
<div class="col-md-10">
#Html.DropDownList("IdTipusInspeccio", null, new {htmlAttributes = new {#class = "form-control"}})
#Html.ValidationMessageFor(model => model.IdTipusInspeccio, "", new {#class = "text-danger"})
</div>
</div>
<div class="form-group" id="criteris"></div>
<div>
<br />
</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("Tornar al llistat d'inspeccions", "Index")
</div>
<script type="text/javascript">
$(document).ready(function () {
$("#IdTipusInspeccio").change(function () {
var categoryId = $("#IdTipusInspeccio").val();
$("#criteris").load('#(Url.Action("CriterisSegonsTipusInspeccio", "Inspeccios", null, Request.Url.Scheme))?idTipusInspeccio=' + categoryId);
});
});
</script>
PartialView CriterisInspeccioAssignatsViewModel:
#model ICollection<Inspeccions.ViewModels.CriterisInspeccioAssignatsViewModel>
#using Inspeccions.Helpers
#foreach (var criteri in Model)
{
<div class="form-horizontal">
<dl class="dl-horizontal">
<dt>
#Html.DisplayNameFor(criter => criteri.Titol)
</dt>
<dd>
#Html.DisplayFor(criter => criteri.Titol)
</dd>
<dt>
#Html.DisplayNameFor(criter => criteri.Descripcio)
</dt>
<dd>
#Html.DisplayFor(criter => criteri.Descripcio)
</dd>
</dl>
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group">
#Html.HiddenFor(criter => criteri.IdCriteri, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.HiddenFor(criter => criteri.IdCriteri, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(criter => criteri.IdCriteri, "", new { #class = "text-danger" })
</div>
</div>
#if (#Html.Action() == "Create" || #Html.Action() == "Edit" || #Html.Action() == "CriterisSegonsTipusInspeccio")
{
<div class="form-group">
#Html.LabelFor(criter => criteri.Assignada, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
#Html.EditorFor(criter => criteri.Assignada)
#Html.ValidationMessageFor(criter => criteri.Assignada, "", new { #class = "text-danger" })
</div>
</div>
</div>
}
#if (#Html.Action() == "Details")
{
<dl class="dl-horizontal">
<dt>
#Html.DisplayNameFor(criter => criteri.Assignada)
</dt>
<dd>
#Html.DisplayFor(criter => criteri.Assignada)
</dd>
</dl>
}
</div>
<hr />
}
If I use it without ajax, having the Criteris fixed it works...
I've search it in Google and I can't find a solution for me.
Can you help me please?
Lot of thanks!

I've finally found the solution!
The problem comes from
public ActionResult CriterisSegonsTipusInspeccio(InspeccioViewModel inspeccioViewModel)
{
ICollection<CriterisInspeccioAssignatsViewModel> criteris = CriterisSegonsTipusInspeccioFalse(inspeccioViewModel.IdTipusInspeccio);
inspeccioViewModel.CriterisInspeccioViewModel = criteris;
return PartialView("CriterisInspeccioAssignatsViewModel", inspeccioViewModel);
}
It have to receive the Model :)

Related

JSON with AJAX not returning value in ASP.NET MVC

Good day guys,
I have a view that i need to search for customer Account details first and not reloading the page.
The user enters the customer Account number and then press the search button.
I have implemented below but it keeps throwing the error part that account number not exist.
// This is the View
#model CreditFacility_Web.Models.CreditFacilityModel.Transaction
#{
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="w3-container" style="padding-left:60px">
#{
ViewBag.Title = "Credit Transaction";
}
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<h2>Credit Transaction</h2>
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(model => model.Account_Number, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Account_Number, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Account_Number, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<button value="Search" class="btn btn-primary">Search</button>
</div>
</div>
<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.Surname, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Surname, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Surname, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Phone_Number, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Phone_Number, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Phone_Number, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Account_Type, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Account_Type, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Account_Type, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Old_Balance, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Old_Balance, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Old_Balance, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Amount, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Amount, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Amount, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.New_Balance, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.New_Balance, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.New_Balance, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Transaction_Type, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Transaction_Type, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Transaction_Type, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Narration, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Narration, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Narration, "", 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-success" />
</div>
</div>
</div>
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>
#section Scripts {
<script type="text/javascript">
$(document).ready(function () {
$(".btn-primary").click(function () {
var accNo = $('#Account_Number').val();
$.ajax({
url: "#Url.Action("AccountDetails", "Transactions")",
type: "POST",
dataType: "json",
data: { accountNo : accNo },
async: false,
error: function () {
alert('Account Number do not Exist Or Other Errors Occurred');
},
success: function (data) {
if (Object.keys(data).length > 0) {
$('#Firstname').val(data.Firstname);
$('#Old_Balance').val(data.Account_Balance);
}
}
});
});
});
</script>
}
Below is the Controller
[HttpPost]
public JsonResult AccountDetails(string accountNo)
{
using (CreditFacilityContext dataContext = new CreditFacilityContext())
{
var accSearchParameter = new SqlParameter("#Account_Number", accountNo);
var accDetails = dataContext.Database.SqlQuery<SavingsAccount>("spGetAccountDetails", accSearchParameter).Select(s => new SavingsAccount
{
Firstname = s.Firstname,
Account_Balance = s.Account_Balance,
//rest of properties
}).SingleOrDefault();
return Json(accDetails, JsonRequestBehavior.AllowGet);
}
}
First thing you want to do is remove that async: false. It does nothing other than tie up the main thread while the transaction is carried out.
I suggest checking that in the HTML that is generated, your input actually has the id of Account_Number and not an auto generated ID. You can manually set an ID of your choosing as well.
The JS should check the value of accNo before sending anything to the backend.

asp.net mvc CRUD add/delete a category/product

ive been stuck on this learning more with CRUD in asp.net mvc. im able to view the category list, view the products list and add just product. I cant figure out how to add to the category and delete from category and product list
1st i wasnt able to view my category list and was about to fix that from data i manually added to the table.
public GenericUnitOfWork _unitOfWork = new GenericUnitOfWork();
public List<SelectListItem> GetCategory()
{
List<SelectListItem> list = new List<SelectListItem>();
var cat = _unitOfWork.GetRepositoryInstance<Tbl_Category>().GetAllRecords();
foreach (var item in cat)
{
list.Add(new SelectListItem { Value = item.CategoryId.ToString(), Text = item.CategoryName });
}
return list;
}
public ActionResult Dashboard()
{
return View();
}
public ActionResult Categories()
{
return View(_unitOfWork.GetRepositoryInstance<Tbl_Category>().GetAllRecords());
}
public ActionResult AddCategory(Tbl_Category tbl)
{
_unitOfWork.GetRepositoryInstance<Tbl_Category>().Add(tbl);
return UpdateCategory(0);
}
public ActionResult UpdateCategory(int categoryId)
{
CategoryDetail cd;
if (categoryId != null)
{
cd = JsonConvert.DeserializeObject<CategoryDetail>(JsonConvert.SerializeObject(_unitOfWork.GetRepositoryInstance<Tbl_Category>().GetFirstorDefault(categoryId)));
}
else
{
cd = new CategoryDetail();
}
return View("UpdateCategory", cd);
}
public ActionResult CategoryEdit(int catId)
{
return View(_unitOfWork.GetRepositoryInstance<Tbl_Category>().GetFirstorDefault(catId));
}
[HttpPost]
public ActionResult CategoryEdit(Tbl_Category tbl)
{
_unitOfWork.GetRepositoryInstance<Tbl_Category>().Update(tbl);
return RedirectToAction("Categories");
}
public ActionResult Product()
{
return View(_unitOfWork.GetRepositoryInstance<Tbl_Product>().GetProduct());
}
public ActionResult ProductEdit(int productId)
{
ViewBag.CategoryList = GetCategory();
return View(_unitOfWork.GetRepositoryInstance<Tbl_Product>().GetFirstorDefault(productId));
}
[HttpPost]
public ActionResult ProductEdit(Tbl_Product tbl, HttpPostedFileBase file)
{
string pic = null;
if (file != null)
{
pic = System.IO.Path.GetFileName(file.FileName);
string path = System.IO.Path.Combine(Server.MapPath("~/ProductImg/"), pic);
// file is uploaded
file.SaveAs(path);
}
tbl.ProductImage = file != null ? pic : tbl.ProductImage;
tbl.ModifiedDate = DateTime.Now;
_unitOfWork.GetRepositoryInstance<Tbl_Product>().Update(tbl);
return RedirectToAction("Product");
}
public ActionResult ProductAdd()
{
ViewBag.CategoryList = GetCategory();
return View();
}
[HttpPost]
public ActionResult ProductAdd(Tbl_Product tbl, HttpPostedFileBase file)
{
string pic = null;
if (file != null)
{
pic = System.IO.Path.GetFileName(file.FileName);
string path = System.IO.Path.Combine(Server.MapPath("~/ProductImg/"), pic);
// file is uploaded
file.SaveAs(path);
}
tbl.ProductImage = pic;
tbl.CreatedDate = DateTime.Now;
_unitOfWork.GetRepositoryInstance<Tbl_Product>().Add(tbl);
return RedirectToAction("Product");
}
here is the updating category view to add a category to the list
<div class="container-fluid">
<!-- Breadcrumbs-->
<ol class="breadcrumb">
<li class="breadcrumb-item">
Dashboard
</li>
<li class="breadcrumb-item ">Categories</li>
<li class="breadcrumb-item active">Add New Category</li>
</ol>
<!-- DataTables Example -->
<div class="card mb-3">
<div class="card-header">
<i class="fas fa-table"></i>
Add New Category
</div>
<div class="card-body">
#using (Html.BeginForm("UpdateCategory", "Admin", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
#*#Html.HiddenFor(m => m.CategoryId)*#
<div class="form-group">
<label class="control-label">Category Id</label>
#Html.TextBoxFor(m => m.CategoryId, new { #class = "form-control", placeholder = "Enter category Id", required = "required", autofocus = "autofocus" })
#Html.ValidationMessageFor(m => m.CategoryId, "", new { #class = "text-danger" })
</div>
<div class="form-group">
<label class="control-label">Category Name</label>
#Html.TextBoxFor(m => m.CategoryName, new { #class = "form-control", placeholder = "Enter category Name", required = "required", autofocus = "autofocus" })
#Html.ValidationMessageFor(m => m.CategoryName, "", new { #class = "text-danger" })
</div>
<a onclick="window.history.back();" class="btn btn-danger">Cancel</a>
<input type="submit" class="btn btn-primary" value="Save" />
}
</div>
</div>
trying to delete category "Category edit view"
<div class="container-fluid">
<ol class="breadcrumb">
<li class="breadcrumb-item">
Category
</li>
<li class="breadcrumb-item active">Edit</li>
</ol>
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
#Html.HiddenFor(model => model.CategoryId)
#{
List<SelectListItem> data = ViewBag.CategoryList;
}
<div class="col-md-6">
<div class="form-group">
#Html.LabelFor(model => model.CategoryName, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.CategoryName, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.CategoryName, "", new { #class = "text-danger" })
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
#Html.LabelFor(model => model.IsActive, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.CheckBox("IsActive")
#Html.ValidationMessageFor(model => model.IsActive, "", new { #class = "text-danger" })
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
#Html.LabelFor(model => model.IsDelete, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.CheckBox("IsDelete")
#Html.ValidationMessageFor(model => model.IsDelete, "", new { #class = "text-danger" })
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-info" />
</div>
</div>
</div>
</div>
}
</div>
trying to delete product but, able to add product and view the list
<div class="container-fluid">
Breadcrumbs
<ol class="breadcrumb">
<li class="breadcrumb-item">
Product
</li>
<li class="breadcrumb-item active">Edit Product</li>
</ol>
</div>
<div class="container-fluid">
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div>
<h4>Product</h4>
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
#Html.HiddenFor(model => model.ProductId, new { htmlAttributes = new { #class = "form-control" } })
<div class="col-md-6">
<div class="form-group">
#Html.LabelFor(model => model.ProductName, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.ProductName, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.ProductName, "", new { #class = "text-danger" })
</div>
</div>
</div>
#{
List<SelectListItem> data = ViewBag.CategoryList;
}
<div class="col-md-6">
<div class="form-group">
#Html.LabelFor(model => model.CategoryId, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownListFor(model => model.CategoryId, data, "---Select---", new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.CategoryId, "", new { #class = "text-danger" })
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
#Html.LabelFor(model => model.IsActive, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.CheckBox("IsActive")
#Html.ValidationMessageFor(model => model.IsActive, "", new { #class = "text-danger" })
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
#Html.LabelFor(model => model.IsDelete, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.IsDelete, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.IsDelete, "", new { #class = "text-danger" })
</div>
</div>
</div>
#Html.HiddenFor(model => model.IsDelete)
<div class="col-md-6">
<div class="form-group">
#Html.LabelFor(model => model.CreatedDate, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.CreatedDate, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.CreatedDate, "", new { #class = "text-danger" })
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
#Html.LabelFor(model => model.ModifiedDate, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.ModifiedDate, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.ModifiedDate, "", new { #class = "text-danger" })
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
#Html.LabelFor(model => model.ProductImage, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.ProductImage, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.ProductImage, "", new { #class = "text-danger" })
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
#Html.LabelFor(model => model.IsFeatured, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.CheckBox("IsFeatured")
#Html.ValidationMessageFor(model => model.IsFeatured, "", new { #class = "text-danger" })
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
#Html.LabelFor(model => model.Quantity, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Quantity, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Quantity, "", new { #class = "text-danger" })
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
#Html.LabelFor(model => model.Price, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Price, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Price, "", new { #class = "text-danger" })
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-info" />
</div>
</div>
</div>
</div>
}
</div>

Issue with centering the elements from html begin form in row

I have a begin form with input elements and text label. For some reason I cannot center the form in the middle of row. I tried text-align center and it centers only the text not buttons or input fields.
or I tried to apply separate css styles on each element but it is messing around the form.
What is wrong with it?
<section class="my-posted-jobs">
<div class="row">
<div class="col-md-6" </div>
<div class="specific-job col-md-6">
<div class="row test">
<div class="form-horizontal">
<h1>Edit Post</h1>
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
#Html.HiddenFor(model => model.JobPostId)
<div class="form-group">
#Html.LabelFor(model => model.Headline, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Headline, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Headline, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.JobAddress, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.JobAddress, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.JobAddress, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.AboutJob, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.AboutJob, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.AboutJob, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.JobCity, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.JobCity, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.JobCity, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.JobPostCode, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.JobPostCode, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.JobPostCode, "", 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>
</div>
</section>

Put two checkboxes horizontally, one beside the other, in bootstrap

I have a form with this code, with two checkboxes at the end of it. What I want is that the checkboxex align horizontally one beside the other:
<div class="form-horizontal">
#Html.HiddenFor(x => x.CategoriaId)
#Html.HiddenFor(x => x.NombreCategoria)
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(model => model.NombreCategoria, "Tipo de Producto", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
<label class="form-control-static" style="color:#ff6a00">#Model.NombreCategoria</label>
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Titulo, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Titulo, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Titulo, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Descripcion, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.TextAreaFor(model => model.Descripcion, new { #class = "form-control", #rows = 3 })
#Html.ValidationMessageFor(model => model.Descripcion, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Precio, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Precio, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Precio, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="">
#Html.LabelFor(model => model.ContactoTelefono, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-2">
#Html.CheckBoxFor(model => model.ContactoTelefono, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.ContactoTelefono, "", new { #class = "text-danger" })
</div>
#Html.LabelFor(model => model.ContactoMail, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-2">
#Html.CheckBoxFor(model => model.ContactoMail, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.ContactoMail, "", new { #class = "text-danger" })
</div>
</div>
</div>
</div>
Now the checkboxes are displayed with a huge space between them. I want to put them more together.
Thank you in advanced.
Like this sample?
HTML:
<div class="form-group">
<input type="checkbox" class="form-control checkbox-inline" />
<input type="checkbox" class="form-control checkbox-inline" />
</div>
CSS:
.checkbox-inline {
display: inline-block;
width: 30px;
height: 30px;
}

How do you call a partial view in a modal div,and passing an object parameter to it?

I have a problem concerning a modal (bootstrap) I want to open. I want to include a partial view.
(I am boing to post my code in a few minutes).
Here is the objective :
1- I have an Index.cshtml page where a link call a javascript
<div class="profile-userbuttons">
#Html.ActionLink("Modifier", "Edit", "Profil", null, new { #class = "modal-link btn btn-success"#*, id = Model.userLog*# })
</div>
(Note that I have commented the second (id) parameter in my anonymous type).
(This parameter should be a object of type userLog).
Then a click on this link call a javascript script :
<!-- Calling Modals -->
<script type="text/javascript">
$(function () {
// Initialize numeric spinner input boxes
//$(".numeric-spinner").spinedit();
// Initialize modal dialog
// attach modal-container bootstrap attributes to links with .modal-link class.
// when a link is clicked with these attributes, bootstrap will display the href content in a modal dialog.
alert(1);
$('body').on('click', '.modal-link', function (e) {
e.preventDefault();
alert(2);
$(this).attr('data-target', '#modal-container');
$(this).attr('data-toggle', 'modal');
alert(3);
});
// Attach listener to .modal-close-btn's so that when the button is pressed the modal dialog disappears
$('body').on('click', '.modal-close-btn', function () {
$('#modal-container').modal('hide');
});
//clear modal cache, so that new content can be loaded
$('#modal-container').on('hidden.bs.modal', function () {
$(this).removeData('bs.modal');
});
$('#CancelModal').on('click', function () {
return false;
});
});
</script>
And of course, the modal div is present in the Index.cshtml :
<!-- Modal -->
<div id="modal-container" class="modal fade"
tabindex="-1" role="dialog">
<div class="modal-content">
</div>
</div>
Remark that the modal is empty. My script is in charge of filling it.
But lets return to my ActionLink. the Action link target a Action : "Edit", in my Controller "Profil"
Here it is :
public ActionResult Edit(/*userslog id*/)
{
return PartialView("Edit"/*,id*/);
}
(I have commented the parameter, to test it was a "parameter passing" issue).
As you read it you see it should return a partial view named "Edit".
here it is :
#model WebSiteModels.Models.userslog
#{
ViewBag.Title = "Edit";
}
<div class="modal-body">
<div class="alert alert-warning">
<h2>Edit</h2>
<h3>YO !</h3>
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>userslog</h4>
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
#Html.HiddenFor(model => model.id)
<div class="form-group">
#Html.LabelFor(model => model.photo, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.photo, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.photo, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.age, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.age, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.age, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.nom, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.nom, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.nom, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.prenom, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.prenom, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.prenom, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.sex, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.sex, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.sex, "", 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.telephone, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.telephone, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.telephone, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.adresse, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.adresse, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.adresse, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.lattitude, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.lattitude, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.lattitude, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.longitude, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.longitude, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.longitude, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.idpersonne, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.idpersonne, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.idpersonne, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.idcontact, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.idcontact, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.idcontact, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.DateCreation, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.DateCreation, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.DateCreation, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.DateModification, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.DateModification, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.DateModification, "", 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>
#Html.ActionLink("Back to List", "Index")
</div>
<div class="row">
<div class="col-md-4 col-md-offset-4">
<button type="button" class="btn btn-default"
data-dismiss="modal">
Cancel
</button>
<button type="submit" id="approve-btn"
class="btn btn-danger">
Save
</button>
</div>
</div>
</div>
</div>
<script type="text/javascript">
$(function () {
$('#approve-btn').click(function () {
$('#modal-container').modal('hide');
});
});
</script>
And when I test it, here is the capture screen I obtain :
Capture screen result : modal seems to be called, but in the browser console, is empty
So here is my problem. My partial view is not called despite the modal is opened. Moreover, when I open it in the browser : every thing appears well.
What do I miss ?
Have you tried to fill the modal content using JQuery with something like(see NEW LINE):
$('body').on('click', '.modal-link', function (e) {
e.preventDefault();
alert(2);
**//// NEW LINE
$(".modal-content").load("/Profil/Edit");
////**
alert(3);
});
and convert the Html.ActionLink to simple <a class='modal-link' data-target='#modal-container' data-toggle='modal'></a> tag with same attributes.

Resources