I have created a user Register form by using custom table (own table) in Umbraco 7.2 MVC.
User registration form created by using petapoco method.
Model code is shown below:
[TableName("UserLogin")]
[PrimaryKey("UserId", autoIncrement = true)]
[ExplicitColumns]
public class User
{
[Column]
public string UserId { get; set;}
[Column]
[Required(ErrorMessage = "Please provide Name", AllowEmptyStrings = false)]
public string Name { get; set; }
[Column]
[Required(ErrorMessage = "Please provide Email",AllowEmptyStrings=false)]
[DataType(DataType.EmailAddress)]
[RegularExpression("^([a-zA-Z0-9_\\-\\.]+)#[a-z0-9-]+(\\.[a-z0-9-]+)*(\\.[a-z]{2,3})$", ErrorMessage = "Email is not a valid e-mail address.")]
public string Email { get; set; }
[Column]
[Required(ErrorMessage = "Please provide Adress", AllowEmptyStrings = false)]
public string Address { get; set; }
[Column]
[RegularExpression(#"^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$", ErrorMessage = "Entered mobile format is not valid.")]
public string Mobile { get; set; }
[Column]
[Required(ErrorMessage = "Please provide Password", AllowEmptyStrings = false)]
[DataType(DataType.Password)]
[StringLength(255, MinimumLength =6, ErrorMessage = "please enter minimum 6 character")]
public string Password { get; set; }
[Required(ErrorMessage = "Please provide Confirm Password", AllowEmptyStrings = false)]
[Compare("Password", ErrorMessage = "Confirm password dose not match.")]
[DataType(DataType.Password)]
[StringLength(255, MinimumLength = 6, ErrorMessage = "please enter minimum 6 character")]
public string UserConfirmPassWord { get; set; }
}
controller(User controller) code is below for insert
public ActionResult AddUser(User _user)
{
//Add new user
if (!ModelState.IsValid)
{
ViewBag.MessageError = "Not Successfully Registration";
return CurrentUmbracoPage();
}
var db = ApplicationContext.Current.DatabaseContext.Database;
_user.Password = Encrypt(_user.Password);//Encrypt the password
db.Insert(_user);
ViewBag.MessageSuccess = "Successfully Registration Done";
return Redirect("/home/login/");
}
Data insert and selectr are working, but I need to edit, update, delete the data by id .
I would need controller and view code for edit, update delete (Umbraco 7.2 using petapoco).
Related
I have a simple order form that I created in mvc and when they hit the submit button, I have the user redirected to paypal for payment and then they get redirected back to the original page. I'm trying to find out how to either pass the model information or save it somehow because after payment is complete, my program sends them an email with a copy of their receipt and successful purchase. How do I go about doing this? Please let me know if there is anything else you need to see. I'm still brand new to MVC and I'm trying to figure this all out.
Model
public class WritingAppModel
{
[Required(ErrorMessage = "Name is required")]
public string Name { get; set; }
[EmailAddress(ErrorMessage = "A Valid Email Address is Required.")]
[Required(ErrorMessage = "Email Address is Required.")]
public string Email { get; set; }
[Phone(ErrorMessage = "A Valid Phone Number is Required.")]
[Required(ErrorMessage = "Phone Number is Required.")]
public string PhoneNumber { get; set; }
[Required(ErrorMessage = "Subject is Required.")]
public string Subject { get; set; }
[Required(ErrorMessage = "Topic is Required.")]
public string Topic { get; set; }
[Required(ErrorMessage = "Document Type is Required.")]
public string DocumentType { get; set; }
[Required(ErrorMessage = "Urgency is Required.")]
public string Urgency { get; set; }
[Required(ErrorMessage = "Number of Pages is Required.")]
public Int16 NumberOfPages { get; set; }
[Required(ErrorMessage = "Requirements are Required.")]
[DataType(DataType.MultilineText)]
[StringLength(200)]
public string Requirements { get; set; }
[Required(ErrorMessage = "Writing Style is Required.")]
public string Style { get; set; }
[Required(ErrorMessage = "Spacing is Required.")]
public string Spacing { get; set; }
[Required(ErrorMessage = "Academic Level is Required.")]
public string AcademicLevel { get; set; }
[Required(ErrorMessage = "Number of Sources is Required.")]
public Int16 NumberOfSources { get; set; }
[Required(ErrorMessage = "Price is Required.")]
[Range(0.01, 10000.00, ErrorMessage = "Your quote is not complete because you haven't completed all of the steps.")]
[DataType(DataType.Currency)]
[DisplayFormat(DataFormatString = "{0:C}")]
public decimal Price { get; set; }
public string UnFormattedPrice
{
get
{
return this.Price.ToString();
}
}
[Required(ErrorMessage = "Currency is Required.")]
public string Currency { get; set; }
}
here i have one model. now i want to buil form using html helper . so when index action will be called then i want to populate model data by hand and send the model to view.
here is my model data but the way i want to build form not being possible just due to lack of knowledge. so if possible help me
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Text.RegularExpressions;
namespace MvcPractise.Models
{
public class Student
{
[Required(ErrorMessage = "First Name Required")] // textboxes will show
[Display(Name = "First Name :")]
[StringLength(5, ErrorMessage = "First Name cannot be longer than 5 characters.")]
public string FirstName { get; set; }
[Required(ErrorMessage = "Last Name Required")] // textboxes will show
[Display(Name = "Last Name :")]
[StringLength(5, ErrorMessage = "Last Name cannot be longer than 5 characters.")]
public string LastName { get; set; }
[Required(ErrorMessage = "DOB require")] // datepicker will show
[Display(Name = "DOB :")]
[DataType(DataType.Date)]
public DateTime Dob { get; set; }
[Required(ErrorMessage = "State Required")] // drodown will show
[Display(Name = "State :")]
public List<State> State { get; set; }
[Required(ErrorMessage = "City Required")] // drodown will show
[Display(Name = "City :")]
public List<City> City { get; set; }
[Required(ErrorMessage = "Language known Required")] // group of checkboxes will show
[Display(Name = "Language known :")]
public List<Language> Language { get; set; }
[Required(ErrorMessage = "Sex Required")] // group of radio button will show
[Display(Name = "Sex :")]
public List<Sex> Sex { get; set; }
[Required(ErrorMessage = "Computer Course Required")] // listbox will show
[Display(Name = "Computer Course Done :")]
public List<ComputerCourse> ComputerCourse { get; set; }
}
public class State
{
public string ID { get; set; }
public string Name { get; set; }
}
public class City
{
public string ID { get; set; }
public string Name { get; set; }
}
public class Language
{
public string ID { get; set; }
public string Name { get; set; }
}
public class Sex
{
public string ID { get; set; }
public string Type { get; set; }
}
public class ComputerCourse
{
public string ID { get; set; }
public string Type { get; set; }
}
}
1) for the first name & last name property i want to show textboxes
2) for the DOB property i want to show textbox with date picker
3) for the DOB property i want to show textbox with date picker
4) for the State/City property i want to show dropdown or combo
5) for the Language property i want to show group of checkboxes
6) for the Sex property i want to show 2 radio button for male & female
7) for the Computer Course property i want to show listbox
now write a index action method which will populate model with dummy data and generate UI.
when click on save button then model data will return back to action method called saved like
public ActionResult Save(Student s)
{
return View(s);
}
or
public ActionResult Save(StudentViewModel sv)
{
return View();
}
please help a newbie to learn things. thanks
Here is a good article about various types of model binding. It's not too long and demystifies the automatic binding and the manual way of doing it.
http://odetocode.com/blogs/scott/archive/2009/04/27/6-tips-for-asp-net-mvc-model-binding.aspx
Also I would agree that this Question is a little too broad in scope. I'll try to help a little. You can use the same method name for the get and post request (The one that loads the empty page is the get, and when you fill out the form you will "post" the form data back to the post method). You will want to specify which one is using http 'get' and which one is using 'post' and you can do so with by decorating your method with an attribute like this.
[HttpGet]
public ActionResult Create()//leave it empty youre about to make it
{
var model = new ObjectType();
return view(model); // pass the new model ( empty object ) to your view;
}
[HttpPost]
public ActionResult Create(ObjectType theObject){
//MVC will try to populate the values of properties
//of theObject if they match the names on your form elements
//save it in your database
}
I have done custommembership as this link:
http://www.brianlegg.com/post/2011/05/09/Implementing-your-own-RoleProvider-and-MembershipProvider-in-MVC-3.aspx
My soulution have two project : CameraStore.Domain and CameraStore.WebUI. In CameraStore.Domain porject, i have an entity named User like this:
public int UserID { get; set; }
[Required]
[Display(Name = "User name")]
public string Username { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[Required]
[Display(Name = "Name")]
public string Name { get; set; }
[Required]
[Display(Name = "City")]
public string City { get; set; }
[Required]
[Display(Name = "Ward")]
public string Ward { get; set; }
[Required]
[Display(Name = "User name")]
public string Address { get; set; }
[Required]
[DataType(DataType.PhoneNumber)]
[Display(Name = "Phone")]
public string Phone { get; set; }
[Required]
[DataType(DataType.EmailAddress)]
[Display(Name = "Email address")]
public string Email { get; set; }
public int RoleID { get; set; }
public virtual Role Role { get; set; }
public virtual ICollection<Order> Order { get; set; }
In my Register Controller method i want to insert user into my database. But i have the field ConfirmPassword, how can i valid it if this property never has in User Entity class. I have used a class named AccountUserModels to return 2 List for 2 dropdownlist in my view, can i add the property ConfirmPassword to this class. I dont want to valid by javascript for ConfirmPassword filed in my view.
Hi if i not wrong you want to compare password right?
try this
[Required(ErrorMessage = "New password is required.")]
[DataType(DataType.Password)]
[StringLength(20, MinimumLength = 5)]
[Display(Name = "New password:")]
public string NewPassword { get; set; }
[Required(ErrorMessage = "Confirm password is required.")]
[DataType(DataType.Password)]
[StringLength(20, MinimumLength = 5)]
[Compare("NewPassword")]
[Display(Name = "Confirm password:")]
public string RePassword { get; set; }
Controller
[HttpPost]
public ActionResult Register(RegisterViewModel model)
{
if (!ModelState.IsValid)
{
return View(model);
}
return RedirectToAction("Success");
}
the model :
public class ChangePasswordModel
{
//User profile key
public string UserName { get; set; }
[Required(ErrorMessage = " Please Enter Current Password ")]
public string OldPassword { get; set; }
[Required(ErrorMessage = " Please Enter a New Password ")]
[StringLength(20, MinimumLength = 6, ErrorMessage = "The {0} must be at least {2} and no longer then {1} characters long.")]
public string NewPassword { get; set; }
[Required(ErrorMessage = " Please Re-enter a New Password ")]
[MustBeValidator(MustBeValidator.Condition.EqualTo, "newpassword", ErrorMessage = "Please, confirm password")]
public string ReNewPassword { get; set; }
}
ajax call to security apicontroller :
[System.Web.Mvc.HttpPost]
public ActionResult ChangePassword(ChangePasswordModel change)
{
if (!ModelState.IsValid)
{
the problem is, even when the "NewPassword" diff from "ReNewPassword" I get ModelState.IsValid = true
I don't know what the MustBeValidator is but you may try using the standard attribute for that in ASP.NET MVC 3:
[Required(ErrorMessage = " Please Re-enter a New Password ")]
[Compare("NewPassword", ErrorMessage = "Please, confirm password")]
public string ReNewPassword { get; set; }
I am using MVC 2.0. I want to set a cooking to login the user for 1 year if user click on the check for login. I dont want user any authentication for this. my model is as follow :
[Required(ErrorMessage = "Please Enter Email Address")]
[DisplayName("User Name")]
public string UserName { get; set; }
[Required(ErrorMessage = "Please Enter Password")]
[DataType(DataType.Password)]
[DisplayName("Password")]
public string Password { get; set; }
[DisplayName("Remember Me")]
public bool RememberMe { get; set; }
and in the view i am using HTML.CkeckboxFor.
Th the post of the controller do this :
if (model.RememberMe == true)
{
Response.Cookies["Login"]["UserName"] = model.UserName;
Response.Cookies["Login"]["Password"] = model.Password;
Response.Cookies["Login"].Expires = DateTime.Now.AddYears(1);
}
and in the get of controller do this :
HttpCookie cookie = Request.Cookies["Login"];
if (cookie != null)
{
model.UserName = cookie.Values[0];
model.Password = cookie.Values[1];
}