how to process hidden values in create method - asp.net

I have this method for proccess "create" form in asp.net:
public ActionResult Create([Bind(Include = "question_id, feedback_id")] feedback_questions feedback_questions)
In form question_id I get from select box, but feedback_id I want to get by hidden value.
My form:
#using (Html.BeginForm()) {
#Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>feedback_questions</h4>
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(model => model.question_id, "choose question: ", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownList("question_id", null, htmlAttributes: new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.question_id, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.feedback_id, "", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
// here I want hidden attribute for feedback_id, which will have value #ViewBag.feedback_id
#Html.ValidationMessageFor(model => model.question_id, "", 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>
}
How can i do it?

Maybe feedback_id is being passed to the view from another view or layout, but since it's on your model, why not just pass it in to the Create view on the Get action
public ActionResult Create()
{
return View(new feedback_questions() { feedback_id = ?? });
}
[HttpPost]
public ActionResult Create([Bind(Include = "question_id, feedback_id")] feedback_questions feedback_questions)
{
return View(feedback_questions);
}
then you can just use the HiddenFor helper
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
#Html.HiddenFor(model => model.feedback_id)
<div class="form-horizontal">
<h4>feedback_questions</h4>
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(model => model.question_id, "choose question: ", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownList("question_id", null, htmlAttributes: new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.question_id, "", 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>
}

You can use Html.Hidden("feedback_id", #ViewBag.feedback_id) where feedback_id is the name of the hidden input, that should match the name of the property you are binding to.

Related

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>

Can't create Model by View and Partialview updated using ajax

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 :)

Form in right side of photo

I'm having some issues working with bootstrap. I'm a little confused right now. I need to add a form on the right side of a photo in Asp.net MVC. I have a render body on my main page and then in the other view I have this bootstrap code:
#using (Html.BeginForm("EditarUtilizador", "Account", null, FormMethod.Post,
new { enctype = "multipart/form-data" }))
{
System.Diagnostics.Debug.WriteLine(#Html.DisplayFor(modelItem => modelItem.ImagePath));
<div class="form-horizontal">
<h4>Editar Perfil Pessoal</h4>
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<fieldset class="col-md-3">
#if (Model.ImagePath != null)
{
<img src="~/Images/#Html.DisplayFor(modelItem => modelItem.ImagePath)" width="150" height="150" />
}
else
{
<img src="~/StaticImages/default-user-image.png" width="150" height="150" />
}
<div class="editor-field">
<input id="ImagePath" title="Upload a product image"
type="file" name="file" />
</div>
</fieldset>
<div class="form-group" >
#Html.LabelFor(m => m.Utilizador, new { #class = "col-md-2 control-label" })
<div class="col-md-10">
#Html.DisplayFor(m => m.Utilizador, new { #class = "form-control"})
#Html.ValidationMessageFor(model => model.Utilizador, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(m => m.Password, new { #class = "col-md-2 control-label" })
<div class="col-md-10">
#Html.EditorFor(m => m.Password, new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.Password, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(m => m.NovaPassword, new { #class = "col-md-2 control-label" })
<div class="col-md-10">
#Html.EditorFor(m => m.NovaPassword, new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.Password, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(m => m.Email, new { #class = "col-md-2 control-label" })
<div class="col-md-10">
#Html.DisplayFor(m => m.Email, new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.Email, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(m => m.Telemóvel, new { #class = "col-md-2 control-label" })
<div class="col-md-10">
#Html.EditorFor(m => m.Telemóvel, new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.Telemóvel, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(m => m.DataNascimento, new { #class = "col-md-2 control-label" })
<div class="col-md-10">
#Html.EditorFor(m => m.DataNascimento, new { #class = "form-control", type = "date", })
#Html.ValidationMessageFor(model => model.DataNascimento, "", new { #class = "text-danger" })
</div>
What I have at the moment is this:
I want to form to appear on the right, but in my case it appears below the image.
Because <div> elements are block-level, they will stack on top of one another by default. Alter the CSS to get what you desire.
Using Bootstrap Grid Mechanism
You can align content in the following manner
#Html.BeginForm(){
<div class="row">
<div class="col-md-8">
Add the input fields in this column
</div>
<div class="col-md-4">
Add the image portion here
</div>
</div>
}
The maximum value of column in Bootrap grid is 12, you can divide this value into individual columns like I did. You can tweak the col-md-* values according to your preference.
You can learn more about the bootstrap Grid in the following link.
https://getbootstrap.com/examples/grid/

Best practice for encrypting user password (with RSA?) in MVC 5?

I want to encrypt the user password before I submit it to the server in MVC 5.
I was thinking if I should use RSA to encrypt/decrypt it or their is a better way prehaps a html helper in MVC 5 for that?
I have this login form:
#model LoginViewModel
<div class="row">
<div class="col-md-8">
<section id="loginForm">
#using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { #class = "form-horizontal", role = "form" }))
{
#Html.AntiForgeryToken()
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(m => m.Email, new { #class = "col-md-2 control-label" })
<div class="col-md-10">
#Html.TextBoxFor(m => m.Email, new { #class = "form-control", id = "Email", Value = "foo#bar.com" })
#Html.ValidationMessageFor(m => m.Email, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(m => m.Password, new { #class = "col-md-2 control-label" })
<div class="col-md-10">
#Html.PasswordFor(m => m.Password, new { #class = "form-control", id = "Password", Value = "baz" })
#Html.ValidationMessageFor(m => m.Password, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Log in" class="btn btn-default" />
</div>
</div>
}
</section>
</div>
</div>
#section Scripts {
#Scripts.Render("~/bundles/jqueryval")
}

Resources