ASP.NET MVC5 - Validate against other table - asp.net-mvc

Using VS2013 and building my first MVC app with EF6 (database first). I have a jobs table and a related items table (there can be millions of records per job). I need to give the user a way to export a subset of items (e.g. item 1,000 - 10,000).
So my controller contains a get method that opens a new view where they can enter the start and end values.
I want to default these to the min and max values from the items table and then I need to validate that the two numbers entered exist in the items table.
Here's my view:
#model PSAMVC.Models.Job
#{
ViewBag.Title = "ExportToLake";
}
<h2>ExportToLake</h2>
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Job</h4>
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
#Html.HiddenFor(model => model.ID)
<div class="form-group">
#Html.LabelFor(model => model.JobNo, "JobNo", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DisplayFor(model => model.JobNo, new { htmlAttributes = new { #class = "form-control" } })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.VersionRef, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DisplayFor(model => model.VersionRef, new { htmlAttributes = new { #class = "form-control" } })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.PSAJobRef, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DisplayFor(model => model.PSAJobRef, new { htmlAttributes = new { #class = "form-control" } })
</div>
</div>
<div class="form-group">
#Html.Label("Start Seq No", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.TextBox("StartSeqNo")
</div>
</div>
<div class="form-group">
#Html.Label("End Seq No", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.TextBox("EndSeqNo")
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Export" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>
#section Scripts {
#Scripts.Render("~/bundles/jqueryval")
}
How/where would I enter the code to validate the two numbers against the items table?
I would think that doing in the view is the best place as the user would get immediate feedback and I can code the controller method knowing it will always be passed valid values.
I could add a view to Db that contains the job no and min and max item no, but it seems like a bit of a hack.
TIA
Mark
Update: here's my Jobs model:
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace PSAMVC.Models
{
using System;
using System.Collections.Generic;
public partial class Job
{
public Job()
{
this.Items = new HashSet<Item>();
this.Reprints = new HashSet<Reprint>();
this.Scans = new HashSet<Scan>();
this.LabelTypes = new HashSet<LabelType>();
}
public int ID { get; set; }
public string JobNo { get; set; }
public string VersionRef { get; set; }
public string PSAJobRef { get; set; }
public int TotalCopies { get; set; }
public int CopiesPerBundle { get; set; }
public int CopiesPerCarton { get; set; }
public int CopiesPerMasterCarton { get; set; }
public Nullable<int> CopiesPerPallet { get; set; }
public int CardType { get; set; }
public string CardTitle { get; set; }
public string CardMMYY { get; set; }
public string StartSerialNo { get; set; }
public int StartBundleNo { get; set; }
public int StartCartonNo { get; set; }
public Nullable<int> StartMasterCartonNo { get; set; }
public Nullable<int> StartPalletNo { get; set; }
public string ProductUPC { get; set; }
public string PackagingUPC { get; set; }
public bool PreProcessed { get; set; }
public bool Completed { get; set; }
public Nullable<int> FormatFileID { get; set; }
public bool UseDummyBarcode { get; set; }
public bool Samples { get; set; }
public string PartNo { get; set; }
public string ProductEAN { get; set; }
public string PONo { get; set; }
public string ImportedFileList { get; set; }
public bool ExportedToLake { get; set; }
public Nullable<int> TotalPalletsOverride { get; set; }
public virtual CardType CardType1 { get; set; }
public virtual FormatFile FormatFile { get; set; }
public virtual ICollection<Item> Items { get; set; }
public virtual SG360JobNos SG360JobNos { get; set; }
public virtual ICollection<Reprint> Reprints { get; set; }
public virtual ICollection<Scan> Scans { get; set; }
public virtual ICollection<LabelType> LabelTypes { get; set; }
}
}
and here's my jobs controller
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using PSAMVC.Models;
using System.Data.SqlClient;
using System.Configuration;
namespace PSAMVC.Controllers
{
public class JobsController : Controller
{
private PSAMVCEntities db = new PSAMVCEntities();
// GET: Jobs
public ActionResult Index()
{
var jobs = db.Jobs.Include(j => j.CardType1).Include(j => j.FormatFile).Include(j => j.SG360JobNos);
return View(jobs.ToList());
}
// GET: Jobs/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Job job = db.Jobs.Find(id);
if (job == null)
{
return HttpNotFound();
}
return View(job);
}
// GET: Jobs/Create
public ActionResult Create()
{
ViewBag.CardType = new SelectList(db.CardTypes, "ID", "Description");
ViewBag.FormatFileID = new SelectList(db.FormatFiles, "ID", "Name");
ViewBag.JobNo = new SelectList(db.SG360JobNos, "JobNo", "JobNo");
return View();
}
// GET: CreateBundlesAndCartons
public ActionResult CreateBandC(Int32 id)
{
string ReturnMessage;
ReturnMessage = "";
using (SqlConnection connection = new SqlConnection())
{
//string connectionStringName = this.DataWorkspace.CooperData.Details.Name;
connection.ConnectionString =
ConfigurationManager.ConnectionStrings["PSAContext"].ConnectionString;
string procedure = "PSA.dbo.CreateBundlesAndCartons";
using (SqlCommand command = new SqlCommand(procedure, connection))
{
command.CommandType = CommandType.StoredProcedure;
command.CommandTimeout = 300;
command.Parameters.Add(
new SqlParameter("#JobID", id));
SqlParameter ErrorString = new SqlParameter("#ErrorString", ReturnMessage);
ErrorString.Direction = ParameterDirection.Output;
ErrorString.Size = 4000;
command.Parameters.Add(ErrorString);
connection.Open();
command.ExecuteNonQuery();
// Save Outout Param
ReturnMessage = ErrorString.Value.ToString();
#ViewBag.Results = ReturnMessage;
}
}
//return Content("You requested the to create bundles and cartons for job ID " + id.ToString() + "<br />Result: " + ReturnMessage + "<br /> Return to Jobs");
return PartialView("_SPResults");
}
// GET: Jobs/ExportToLake/5
public ActionResult ExportToLake(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Job job = db.Jobs.Find(id);
if (job == null)
{
return HttpNotFound();
}
ViewBag.JobNo = new SelectList(db.SG360JobNos, "JobNo", "JobNo", job.JobNo);
return View(job);
}
// GET: ExportToLake1
public ActionResult ExportToLake1(Int32 id, Int64 StartSeqNo, Int64 EndSeqNo, Boolean ReverseOrder, String FileNameSuffix)
{
string ReturnMessage;
ReturnMessage = "";
using (SqlConnection connection = new SqlConnection())
{
//string connectionStringName = this.DataWorkspace.CooperData.Details.Name;
connection.ConnectionString =
ConfigurationManager.ConnectionStrings["PSAContext"].ConnectionString;
string procedure = "PSA.dbo.ExportToLakeBulk";
using (SqlCommand command = new SqlCommand(procedure, connection))
{
command.CommandType = CommandType.StoredProcedure;
command.CommandTimeout = 1200;
command.Parameters.Add(
new SqlParameter("#JobID", id));
command.Parameters.Add(
new SqlParameter("#ReverseOrder", ReverseOrder));
command.Parameters.Add(
new SqlParameter("#StartSeqNo", StartSeqNo));
command.Parameters.Add(
new SqlParameter("#EndSeqNo", EndSeqNo));
command.Parameters.Add(
new SqlParameter("#Suffix", FileNameSuffix));
SqlParameter ErrorString = new SqlParameter("#ErrorString", ReturnMessage);
ErrorString.Direction = ParameterDirection.Output;
ErrorString.Size = 4000;
command.Parameters.Add(ErrorString);
connection.Open();
command.ExecuteNonQuery();
// Save Outout Param
ReturnMessage = ErrorString.Value.ToString();
#ViewBag.Results = ReturnMessage;
}
}
//return Content("You requested the to create bundles and cartons for job ID " + id.ToString() + "<br />Result: " + ReturnMessage + "<br /> Return to Jobs");
return PartialView("_SPResults");
}
// POST: Jobs/ExportToLake
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult ExportToLake2([Bind(Include = "ID,StartSeqNo,EndSeqNo,ReverseOrder")] Job job)
{
if (ModelState.IsValid)
{
//db.Jobs.Add(job);
//db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.CardType = new SelectList(db.CardTypes, "ID", "Description", job.CardType);
ViewBag.FormatFileID = new SelectList(db.FormatFiles, "ID", "Name", job.FormatFileID);
ViewBag.JobNo = new SelectList(db.SG360JobNos, "JobNo", "JobNo", job.JobNo);
return View(job);
}
// POST: Jobs/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "ID,JobNo,VersionRef,PSAJobRef,TotalCopies,CopiesPerBundle,CopiesPerCarton,CopiesPerMasterCarton,CopiesPerPallet,CardType,CardTitle,CardMMYY,StartSerialNo,StartBundleNo,StartCartonNo,StartMasterCartonNo,StartPalletNo,ProductUPC,PackagingUPC,PreProcessed,Completed,FormatFileID,UseDummyBarcode,Samples,PartNo,ProductEAN,PONo,ImportedFileList,ExportedToLake,TotalPalletsOverride")] Job job)
{
if (ModelState.IsValid)
{
db.Jobs.Add(job);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.CardType = new SelectList(db.CardTypes, "ID", "Description", job.CardType);
ViewBag.FormatFileID = new SelectList(db.FormatFiles, "ID", "Name", job.FormatFileID);
ViewBag.JobNo = new SelectList(db.SG360JobNos, "JobNo", "JobNo", job.JobNo);
return View(job);
}
// GET: Jobs/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Job job = db.Jobs.Find(id);
if (job == null)
{
return HttpNotFound();
}
ViewBag.CardType = new SelectList(db.CardTypes, "ID", "Description", job.CardType);
ViewBag.FormatFileID = new SelectList(db.FormatFiles, "ID", "Name", job.FormatFileID);
ViewBag.JobNo = new SelectList(db.SG360JobNos, "JobNo", "JobNo", job.JobNo);
return View(job);
}
// POST: Jobs/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "ID,JobNo,VersionRef,PSAJobRef,TotalCopies,CopiesPerBundle,CopiesPerCarton,CopiesPerMasterCarton,CopiesPerPallet,CardType,CardTitle,CardMMYY,StartSerialNo,StartBundleNo,StartCartonNo,StartMasterCartonNo,StartPalletNo,ProductUPC,PackagingUPC,PreProcessed,Completed,FormatFileID,UseDummyBarcode,Samples,PartNo,ProductEAN,PONo,ImportedFileList,ExportedToLake,TotalPalletsOverride")] Job job)
{
if (ModelState.IsValid)
{
db.Entry(job).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.CardType = new SelectList(db.CardTypes, "ID", "Description", job.CardType);
ViewBag.FormatFileID = new SelectList(db.FormatFiles, "ID", "Name", job.FormatFileID);
ViewBag.JobNo = new SelectList(db.SG360JobNos, "JobNo", "JobNo", job.JobNo);
return View(job);
}
// GET: Jobs/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Job job = db.Jobs.Find(id);
if (job == null)
{
return HttpNotFound();
}
return View(job);
}
// POST: Jobs/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Job job = db.Jobs.Find(id);
db.Jobs.Remove(job);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}

Firstly the server side code should not assume that the values being passed are valid. Always validate the values and handle errors correctly. Client side validation can be bypassed.
In terms of providing instant feedback, one approach is to have an action on a controller that accepts the value to validate as a parameter and returns json containing whether the value was valid and if not the error.
This action can then be called on the input fields blur event or change even providing close to real time feedback on whether the values are valid.
Another approach is to have the valid values determined during the page rendering process and embedded into the client side validation framework if you have one (or use custom JS).
Client Code
function performValidate(data, url) {
var result = $.ajax({
type: "POST",
url: url,
data: data,
success: function (data) {
if (!data.success) {
//HandleIncorrectValue
}
//HandleCorrectValue
},
error: function (data) {
//HandleError
}
});
Controller Code
[HttpPost]
public ActionResult Validate(int value)
{
var response = ValidateValue(value);
return Json(new { success = response.Success, message = response.Message });
}

Related

how to upload image in ActionResult asp.net mvc?

I want set image to my post but I can't.all model field is fill but image field is null
my model class:
public class Game
{
public Game()
{
Platforms = new List<Platform>();
}
[Key]
public int GameID { get; set; }
public string Name { get; set; }
public string Image { get; set; }
[AllowHtml]
public string Description { get; set; }
public DateTime PublishDate { get; set; }
public virtual ICollection<Platform> Platforms { get; set; }
}
my controller class :
public ActionResult Create_post(Game model,int[] platformsIds)
{
if (ModelState.IsValid)
{
foreach (var pId in platformsIds)
{
var platform = _repository.Get<Platform>(pId);
model.Platforms.Add(platform);
}
_repository.Add<Game>(model);
return RedirectToAction("Index");
}
ViewBag.Platforms = _repository.GetAll<Platform>().ToList();
ViewBag.HtmlContent = model.Description;
return View(model);
}
thank you
you just check image url before save your model
in your view you must have this code
<div class="form-group">
#Html.LabelFor(model => model.Image, htmlAttributes: new { #class = "control-label col-md-1" })
<div class="col-md-4">
<div class="col-md-6 col-md-push-0">
<input type="file" class="form-control" name="ImageUrl" id="ImageUrl" />
</div>
#Html.ValidationMessageFor(model => model.Image, "", new { #class = "text-danger" })
</div>
</div>
in your controller you must change your code
public ActionResult Create_post(Game model,int[] platformsIds,HttpPostedFileBase ImageUrl)
{
if (ModelState.IsValid)
{
foreach (var pId in platformsIds)
{
var platform = _repository.Get<Platform>(pId);
model.Platforms.Add(platform);
}
if (ImageUrl != null && ImageUrl.ContentLength > 0)
{
var uploadDir = "~/images/Games/"; // your location for save images
string image = Guid.NewGuid().ToString() + ImageUrl.FileName;
var imagePath = Path.Combine(Server.MapPath(uploadDir),image);
ImageUrl.SaveAs(imagePath);
model.Image = image;
}
_repository.Add<Game>(model);
return RedirectToAction("Index");
}
ViewBag.Platforms = _repository.GetAll<Platform>().ToList();
ViewBag.HtmlContent = model.Description;
return View(model);
}

The ViewData item that has the key 'Position' is of type 'System.String' but must be of type 'IEnumerable<SelectListItem>' [duplicate]

This question already has answers here:
The ViewData item that has the key 'XXX' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'
(6 answers)
Closed 6 years ago.
I am populating DropDownList from in-memory data and getting this error on POST.
The ViewData item that has the key 'Position' is of type 'System.String' but must be of type 'IEnumerable'.
Model:
public class StaffModel
{
public int id { get; set; }
public string Email { get; set; }
[DataType(DataType.Password)]
public string Password { get; set; }
[DataType(DataType.Password)]
public string PasswordConfirm { get; set; }
public string Emp_Name { get; set; }
public string Emp_Address { get; set; }
public string Phone { get; set; }
public string Position { get; set; }
public List<SelectListItem> Positions { set; get; }
}
Controller:
public ActionResult Register()
{
IEnumerable<SelectListItem> position = db.Positions.Select(p => new SelectListItem
{
Text = p.Position_Title,
Value = p.Position_ID.ToString()
});
ViewBag.Position = position;
return View();
}
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(StaffModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
Employee em = new Employee
{
Employee_Name = model.Emp_Name,
Address = model.Emp_Address,
Phone = model.Phone,
Position_ID = Convert.ToInt32(db.Positions.Where(p => p.Position_Title == model.Position).Select(p => p.Position_ID)),
};
db.Employees.Add(em);
db.SaveChanges();
return RedirectToAction("Index", "Employees");
}
}
return View(model);
}
enter code here
HTML/Razor:
<div class="form-group">
#Html.LabelFor(model => model.Position, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownList("Position",null, htmlAttributes: new { #class = "form-control" } )
#Html.ValidationMessageFor(model => model.Position, "", new { #class = "text-danger" })
</div>
</div>
Your binding on the drop down list doesn't look right.
You're model contains Positions which is the data for your drop down. You also have "Position" which I presume is a string that will be bound to the selected value in the drop down.
Change your view to
#Html.DropDownListFor(x=> x.Position, Model.Positions, new {#class = "form-control"}) )
Then on your post, it looks as if you are trying to get the value selected from the drop down by performing a linq query on the "Positions" list of the model which was used to populate your dropdown. You should now have the selected value in the "Position" property of your model.
So you should say
Position_ID = model.Position
I'd also use a model for your Register View.
Something like...
public class RegisterViewModel
{
public IEnumerable<SelectListItem> Positions { get; set; }
public string Position { get; set; }
}
Plus the additional fields you need.
The in your Register action method, populate the view model and return the view.
public ActionResult Register()
{
var regModel = new RegisterViewModel
{
Positions = db.Positions.Select(p => new SelectListItem
{
Text = p.Position_Title,
Value = p.Position_ID.ToString()
})
};
return View("Register",regModel);
}
You don't need to use ViewBag now.
Hope that helps
it work correctly. thank everybody help me:D
Model:
public class StaffModel
{
public string Position { get; set; }
public List<Position> Positions { set; get; }
public int selectID { get; set; }
}
Controller:
public ActionResult Register()
{
StaffModel st = new StaffModel();
st.Positions = db.Positions.ToList();
return View(st);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Register(StaffModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
var result = UserManager.Create(user, model.Password);
if (result.Succeeded)
{
Employee em = new Employee
{
Employee_Name = model.Emp_Name,
Address = model.Emp_Address,
Phone = model.Phone,
Position_ID = model.selectID,
ID_User = user.Id
};
db.Employees.Add(em);
db.SaveChanges();
return RedirectToAction("Index", "Employees");
}
else
AddErrors(result);
}
ViewBag.Position = new SelectList(db.Positions, "Position_ID", "Position_Title");
return View(model);
}
HTML/Razor:
<div class="form-group">
#Html.LabelFor(model => model.Position, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownListFor(model=>model.selectID,new SelectList(Model.Positions, "Position_ID", "Position_Title"), htmlAttributes: new { #class = "form-control" } )
#Html.ValidationMessageFor(model => model.Position, "", new { #class = "text-danger" })
</div>
</div>

Values are null when I wanna edit DB in ASP.NET MVC

I have a problem with my project in ASP.NET Model-View-Controller.
I have an application "Guestbook". I can successfully create guests, but I cannot edit them.
Look:
1. I start application - all ok
2. I create a guest - all ok (guest has all the entered data)
3. I click "edit" - all ok
4. I see new window. All fields on the screen have correct data, but I have a breakpoint in edit function and I see that all variables are null.
Model:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;
namespace Guestbook.Models
{
public class Guest
{
public int ID { get; set; }
[Display(Name = "name", ResourceType=typeof(Resources.Resources))]
[Required(ErrorMessageResourceType = typeof(Resources.Resources),
ErrorMessageResourceName = "nameRequired")]
[StringLength(50, ErrorMessageResourceType = typeof(Resources.Resources),
ErrorMessageResourceName = "nameLong")]
public string name { get; set; }
[Display(Name = "surname", ResourceType = typeof(Resources.Resources))]
[Required(ErrorMessageResourceType = typeof(Resources.Resources),
ErrorMessageResourceName = "surnameRequired")]
[StringLength(50, ErrorMessageResourceType = typeof(Resources.Resources),
ErrorMessageResourceName = "surnameLong")]
public string surname { get; set; }
[Display(Name = "firm", ResourceType = typeof(Resources.Resources))]
[Required(ErrorMessageResourceType = typeof(Resources.Resources),
ErrorMessageResourceName = "firmRequired")]
[StringLength(50, ErrorMessageResourceType = typeof(Resources.Resources),
ErrorMessageResourceName = "firmLong")]
public string firm { get; set; }
[Display(Name = "toWhom", ResourceType = typeof(Resources.Resources))]
[Required(ErrorMessageResourceType = typeof(Resources.Resources),
ErrorMessageResourceName = "toWhomRequired")]
[StringLength(50, ErrorMessageResourceType = typeof(Resources.Resources),
ErrorMessageResourceName = "toWhomLong")]
public string toWhom { get; set; }
[Display(Name = "permitNumber", ResourceType = typeof(Resources.Resources))]
[Required(ErrorMessageResourceType = typeof(Resources.Resources),
ErrorMessageResourceName = "permitRequired")]
[Range(0, 10000, ErrorMessageResourceType = typeof(Resources.Resources),
ErrorMessageResourceName = "permitRange")]
public int permitNumber { get; set; }
[Display(Name = "magazine", ResourceType = typeof(Resources.Resources))]
[Required(ErrorMessageResourceType = typeof(Resources.Resources),
ErrorMessageResourceName = "magazineRequired")]
public bool magazine { get; set; }
[Display(Name = "entranceTime", ResourceType = typeof(Resources.Resources))]
[DataType(DataType.Date)]
public DateTime? entranceTime { get; set; }
[Display(Name = "entranceTimeTime", ResourceType = typeof(Resources.Resources))]
public string entranceTimeTime { get; set; }
[Display(Name = "exitDate", ResourceType = typeof(Resources.Resources))]
[DataType(DataType.Date)]
public DateTime? exitDate { get; set; }
[Display(Name = "exitTime", ResourceType = typeof(Resources.Resources))]
public string exitTime { get; set; }
}
public class GuestDBContext : DbContext
{
public DbSet<Guest> Guests { get; set; }
}
}
Controller:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using Guestbook.Models;
namespace Guestbook.Controllers
{
public class GuestsController : BaseController
{
private GuestDBContext db = new GuestDBContext();
// GET: Guests
public ActionResult Index()
{
//Sortowanie po nazwisku.
var guests = from s in db.Guests select s;
guests = guests.OrderBy(s => s.surname);
return View(guests.ToList());
}
// GET: Guests/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Guest guest = db.Guests.Find(id);
if (guest == null)
{
return HttpNotFound();
}
return View(guest);
}
// GET: Guests/Create
public ActionResult Create()
{
return View();
}
// POST: Guests/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "ID,name,surname,firm,toWhom,permitNumber,magazine,entranceTime,entranceTimeTime,exitDate,exitTime")] Guest guest)
{
guest.entranceTime = DateTime.Today;
guest.exitDate = DateTime.Today;
DateTime dt = DateTime.Now;
string format = "HH:mm:ss";
guest.entranceTimeTime = dt.ToString(format);
guest.exitTime = dt.ToString(format);
if (ModelState.IsValid)
{
db.Guests.Add(guest);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(guest);
}
// GET: Guests/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Guest guest = db.Guests.Find(id);
if (guest == null)
{
return HttpNotFound();
}
return View(guest);
}
// POST: Guests/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "exitDate,exitTime")] Guest g)
{
g.exitDate = DateTime.Today;
DateTime dt = DateTime.Now;
string format = "HH:mm:ss";
g.exitTime = dt.ToString(format);
if (ModelState.IsValid)
{
db.Entry(g).State = EntityState.Modified;
db.Entry(g).Property(gg => gg.name).IsModified = false;
db.Entry(g).Property(gg => gg.surname).IsModified = false;
db.Entry(g).Property(gg => gg.firm).IsModified = false;
db.Entry(g).Property(gg => gg.permitNumber).IsModified = false;
db.Entry(g).Property(gg => gg.magazine).IsModified = false;
db.Entry(g).Property(gg => gg.toWhom).IsModified = false;
db.Entry(g).Property(gg => gg.entranceTime).IsModified = false;
db.Entry(g).Property(gg => gg.entranceTimeTime).IsModified = false;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(g);
}
// GET: Guests/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Guest guest = db.Guests.Find(id);
if (guest == null)
{
return HttpNotFound();
}
return View(guest);
}
// POST: Guests/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Guest guest = db.Guests.Find(id);
db.Guests.Remove(guest);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}
View:
#model Guestbook.Models.Guest
#{
ViewBag.Title = #Resources.exitGuest;
}
<h2>#Resources.exitGuest</h2>
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
#Html.HiddenFor(model => model.ID)
<div class="form-group">
#Html.LabelFor(model => model.name, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DisplayFor(model => model.name, new { htmlAttributes = new { #class = "form-control" } })
</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.DisplayFor(model => model.surname, new { htmlAttributes = new { #class = "form-control" } })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.firm, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DisplayFor(model => model.firm, new { htmlAttributes = new { #class = "form-control" } })
</div>
</div>
<br />
<div class="form-group">
<div class="col-md-10">
<b>#Resources.leftOk</b>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value=#Resources.y class="btn btn-default" />
<input type="button" value=#Resources.cancel onclick="location.href='#Url.Action("","guests")'" class="btn btn-default" />
</div>
</div>
</div>
}
#section Scripts {
#Scripts.Render("~/bundles/jqueryval")
}
Firstly in your view you are only outputting a single form variable which is the ID parameter. All of the other model properties are being displayed only. You either need to output them as, for example, EditorFor or HiddenFor to get them posted back.
Secondly in your Edit method, the method signature is this:
public ActionResult Edit([Bind(Include = "exitDate,exitTime")] Guest g)
The Bind attribute is telling the MVC model binder to only bind the exitDate and exitTime properties so everything else will show as null. You can either remove the attribute:
public ActionResult Edit(Guest g)
Or add in the other properties that you require to be passed in:
public ActionResult Edit([Bind(Include = "exitDate,exitTime,name,surname,etc...")] Guest g)
So if you combine those two problems you will see that even the ID property isn't getting passed through and everything is null.
use
#Html.TextBoxFor(...
instead of
#Html.DisplayFor(....
That way the view will then pass them to the controller 'edit' HttpPost ActionResult after form submission.
The
#Html.DisplayFor(...
Is only used for outputting the model to the screen. It does not, however, allow them to be edited in this way (Hence use the TextBoxFor or EditorFor instead)
I'm not too sure if you want the 'exitDate' and exitTime to be manually edited or not, but if you do, then you will need to add them to your view, as well as including them in your Binding Property after adding them to your model.
If you do, then using something like:
(Guest guest)
instead of binding the data
Should include a binding for that.

How to change the text of a drop dowlist?

I have a dropdowlist in my view
<div class="editor-label">
#Html.LabelFor(model => model.SubProcessId, "SubProcess")
</div>
<div class="editor-field">
#Html.DropDownList("SubProcessId", ViewBag.SubProcessId as SelectList)
#Html.ValidationMessageFor(model => model.SubProcessId)
</div>
That has this model
namespace CTTModel
{
using System;
using System.Collections.Generic;
public partial class **SubProcess**
{
public SubProcess()
{
this.Risks = new HashSet<Risk>();
}
public int Id { get; set; }
public string Name { get; set; }
public int ProcessId { get; set; }
public string Description { get; set; }
public virtual **Process** Process { get; set; }
public virtual ICollection<Risk> Risks { get; set; }
}
}
which appears in the view via this controller
public ActionResult Create()
{
try
{
// Risk
**ViewBag.SubProcessId = new SelectList(_db.SubProcesses, "Id", "Name", "Process");**
ViewBag.RiskSeverityId = new SelectList(_db.RiskSeverities, "Id", "Name");
ViewBag.RiskFrequencyId = new SelectList(_db.RiskFrequencies, "Id", "Name");
ViewBag.RiskRatingId = new SelectList(_db.RiskRatings, "Id", "Name");
ViewBag.RiskType1Id = new SelectList(_db.RiskType1, "Id", "Name");
ViewBag.RiskType2Id = new SelectList(_db.RiskType2, "Id", "Name");
//Test
ViewBag.RegionLevelId = new SelectList(_db.RegionLevels, "Id", "Name");
}
catch (Exception ex)
{
throw;
}
return View();
}
These are the suprocesses that show in the dropdownlist
Past due collection
Dealer audits
Credit Watch classification
Reconciliation
Control
Every sub process can have on process
Portfolio Management
Finance
Test Modeling
Now I was asked to show in the dropdownlist not the subprocess name, but instead
"<process name> - <subprocess name>"
Portfolio Management - Reconciliation, for example
And I have the faintest idea how to do that.
Any ideas??
var subProcessList = _db.SubProcesses
.Select(x => new
{
Id = x.Id,
Name = string.Format("{0} - {1}", x.Process.Name, x.Name)
})
.ToList();
ViewBag.SubProcessId = new SelectList(subProcessList, "Id", "Name");

Passing Model Object Data from View, to Controller, to Model?

I'm brand new to ASP.net MVC (About a week in), so there is still quite a bit of confusion...
How do I go about passing the current views model into the controller so I can get at the model data?
View
#model KineticBomardment.Models.Blog.BlogPost
#{
ViewBag.Title = "Index";
}
#using (Html.BeginForm())
{
#Html.ValidationSummary(false)
<fieldset class="block">
<div class="input">
#Html.LabelFor(x => x.Title)
#Html.EditorFor(x => x.Title)
</div>
<div class="input">
#Html.LabelFor(x => x.ShortDescription)
#Html.EditorFor(x => x.ShortDescription)
</div>
<div class="button">
#Html.ActionLink("Submit", "CreateNewBlogEntry", "Admin" );
</div>
</fieldset>
}
I then have a controller of
public ActionResult CreateNewBlogEntry(Models.Blog.BlogPost currentBlogModel)
{
if (ModelState.IsValid)
{
currentBlogModel.CreatePost();
return Content("Created!");
}
return View();
}
And a model of
public class BlogPost
{
public int Id { get; set; }
[Required]
[Display(Name="Title of Blog Post")]
public string Title { get; set; }
public DateTime DateCreated { get; set; }
[Required]
[Display(Name = "Short Description")]
public string ShortDescription { get; set; }
public string LongDescription { get; set; }
public int HeaderImage { get; set; }
public ICollection<BlogPost> GetAllPosts()
{
ICollection<BlogPost> posts = new List<BlogPost>();
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["KineticBombardment"].ToString()))
{
using (SqlCommand cmd = new SqlCommand("select title, datecreated, shortdescription from blogentries where id > 0", connection))
{
cmd.Parameters.Clear();
connection.Open();
cmd.ExecuteNonQuery();
using (SqlDataReader reader = cmd.ExecuteReader())
{
while(reader.Read())
{
this.ShortDescription = reader["shortdescription"].ToString();
this.Title = reader["title"].ToString();
this.DateCreated = Convert.ToDateTime(reader["datecreated"].ToString());
posts.Add(this);
}
}
return posts;
}
}
}
public void CreatePost()
{
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["KineticBombardment"].ToString()))
{
using (SqlCommand cmd = new SqlCommand("insert into blogentries (shortdescription, datecreated, blogtype, title) values (#shortdescription, #datecreated, #blogtype, #title)", connection))
{
cmd.Parameters.Clear();
connection.Open();
cmd.Parameters.Add("#shortdescription", SqlDbType.VarChar, 500).Value = this.ShortDescription;
cmd.Parameters.Add("#datecreated", SqlDbType.DateTime).Value = DateTime.Now;
cmd.Parameters.Add("#blogtype", SqlDbType.Int).Value = 1;
cmd.Parameters.Add("#title", SqlDbType.VarChar, 255).Value = this.Title;
cmd.ExecuteNonQuery();
}
}
}
}
Change:
<div class="button">
#Html.ActionLink("Submit", "CreateNewBlogEntry", "Admin" );
</div>
To:
<input type="submit" class="button" />
and
public ActionResult CreateNewBlogEntry(Models.Blog.BlogPost currentBlogModel)
{
if (ModelState.IsValid)
{
currentBlogModel.CreatePost();
return Content("Created!");
}
return View();
}
to
public ActionResult CreateNewBlogEntry()
{
return View();
}
[HttpPost]
public ActionResult CreateNewBlogEntry(Models.Blog.BlogPost model)
{
if (ModelState.IsValid)
{
currentBlogModel.CreatePost();
return Content("Created!");
}
return View();
}
I've made some assumptions, but that should work

Resources