Captcha is not loading While adding Screen restriction - asp.net-mvc-4

I am developing my project in MVC. In registration page i am using captcha control. It is working Fine. But my problem is i am adding screen restriction to some of my pages. While adding screen restriction captcha is not getting loaded.
can anyone please help me?
I have added the screen restriction code below
public class ScreenRestriction : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var routeData = filterContext.HttpContext.Request.RequestContext.RouteData;
string currentAction = routeData.GetRequiredString("action");
string currentController = routeData.GetRequiredString("controller");
string currentArea = routeData.Values["area"] as string;
var request = filterContext.HttpContext.Request;
var response = filterContext.HttpContext.Response;
var session = filterContext.HttpContext.Session;
if ((currentAction != "Login" && currentAction != "Create" && currentAction != "GetUser" && currentAction != "Index" ) == true)
{
if (session["UserID"] == null)
{
if (request.IsAjaxRequest())
{
response.StatusCode = 590;
}
else
{
var url = new UrlHelper(filterContext.HttpContext.Request.RequestContext);
response.Redirect(url.Action("Login", "CU"));
}
filterContext.Result = new EmptyResult();
}
else
{
if (!filterContext.HttpContext.Request.IsAjaxRequest())
{
}
}
}
base.OnActionExecuting(filterContext);
}
Screen restriction in Filters
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
filters.Add(new ScreenRestriction());
}
}
I have added the registration page code below
View--
#model SYTMain.Models.tblUser
#using CaptchaMvc.HtmlHelpers
#using CaptchaMvc;
#{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Layout_for_registration.cshtml";
}
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
#Html.ValidationSummary(true)
<fieldset>
<legend>tblUser</legend>
<div class="editor-label">
#Html.LabelFor(model => model.FirstName)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.FirstName)
#Html.ValidationMessageFor(model => model.FirstName)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.MiddleName)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.MiddleName)
#Html.ValidationMessageFor(model => model.MiddleName)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.LastName)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.LastName)
#Html.ValidationMessageFor(model => model.LastName)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.EmailID)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.EmailID)
#Html.ValidationMessageFor(model => model.EmailID)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Password)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Password)
#Html.ValidationMessageFor(model => model.Password)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.AddressId, "Address1")
</div>
<div class="editor-field">
#Html.EditorFor(model => model.tblAddress.Address1)
#Html.ValidationMessageFor(model => model.tblAddress.Address1)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.AddressId, "Address2")
</div>
<div class="editor-field">
#Html.EditorFor(model => model.tblAddress.Address2)
#Html.ValidationMessageFor(model => model.tblAddress.Address2)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.MobileNumber)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.MobileNumber)
#Html.ValidationMessageFor(model => model.MobileNumber)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.WorkNumber)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.WorkNumber)
#Html.ValidationMessageFor(model => model.WorkNumber)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.HomeNumber)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.HomeNumber)
#Html.ValidationMessageFor(model => model.HomeNumber)
</div>
#Html.Captcha(3)
<br />
<p class="Error"> #ViewBag.ErrMessage </p>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
Controller
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(tblUser tbluser)
{
string val = System.Configuration.ConfigurationManager.AppSettings["CUTypeId"];
int code = Convert.ToInt32(val);
Session["Emailid"] = tbluser.EmailID.Trim();
if (ModelState.IsValid && this.IsCaptchaValid("Captcha Not Valid") )
{
EmailManager.SendConfirmationEmailName(tbluser.EmailID, tbluser.FirstName);
tbluser.UserTypeId = code;
db.tblUsers.Add(tbluser);
db.SaveChanges();
return RedirectToAction("Login", new { Email = tbluser.EmailID });
}
ViewBag.BusinessCategoryId = new SelectList(db.tblBusinessCategories, "BusinessID", "BusinessName", tbluser.BusinessCategoryId);
ViewBag.UserTypeId = new SelectList(db.tblUserTypes, "UserTypeID", "UserType", tbluser.UserTypeId);
ViewBag.AddressId = new SelectList(db.tblAddresses, "AddressID", "Address1", tbluser.AddressId);
ViewBag.ErrMessage = "Error: Captcha not valid";
return View(tbluser);
}

Related

bootstrap form alignment with multiple controls

Hello all I am having my design as follows in my html
#model PPM.Models.ProjectModel
<div class="panel-heading panel-head">Create New Project</div>
<br />
<div class="container">
<div class="form-horizontal">
#using (Ajax.BeginForm("Invite", "Invite", null, new AjaxOptions() { HttpMethod = "post", LoadingElementId = "divLoading", OnSuccess = "getresult" }, null))
{
<div class="form-group">
<label for="ProjectName" class="col-sm-4">Project Name</label>
#Html.EditorFor(model => model.ProjectName, "", new { htmlAttributes = new { #class = "form-control", #placeholder = "Project Name" } })
#Html.ValidationMessageFor(model => model.ProjectName, "", new { #class = "text-danger" })
</div>
<div class="form-group">
<label for="ManagerId" class="col-sm-4">Project Manager</label>
#Html.DropDownListFor(Model => Model.ManagerId, Model.ManagerList, "--SELECT--", new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.ManagerId, "", new { #class = "text-danger" })
</div>
<div class="control-group">
<label for="Duration">Duration </label>
#Html.EditorFor(model => model.Duration, "", new { htmlAttributes = new { #class = "form-control", #placeholder = "Duration" } })
#Html.ValidationMessageFor(model => model.Duration, "", new { #class = "text-danger" })
#Html.DropDownListFor(Model => Model.DurationId, Model.DurationTypes, "--SELECT--", new { #class = "form-control input-xs" })
#Html.ValidationMessageFor(model => model.DurationId, "", new { #class = "text-danger" })
</div>
<div class="form-group">
<label for="NoOfResources" class="col-sm-4">No Of Resources</label>
#Html.EditorFor(model => model.NoOfResourses, "", new { htmlAttributes = new { #class = "form-control", #placeholder = "Number of resources" } })
</div>
<div class="form-group">
<label for="StartDate" class="col-sm-4">Start Date</label>
#Html.EditorFor(model => model.StartDate, "", new { htmlAttributes = new { #class = "form-control", #placeholder = "Number of resources" } })
</div>
<div class="form-group">
<label for="EndDate" class="col-sm-4">End Date</label>
#Html.EditorFor(model => model.EndDate, "", new { htmlAttributes = new { #class = "form-control", #placeholder = "Number of resources" } })
</div>
}
</div>
</div>
I am getting form as expected but I am unable to align one control inline. can some one help me. I would like to see them in one line
It looks like you aren't using columns for the 'Duration' label so what you have is:
<label for="Duration">Duration </label>
When it should be:
<label for="Duration" class="col-sm-4">Duration </label>
like the rest of your labels.

How to add more fields to contact us page in nop commerce?

How can I add more fields to "contact us" form in nop commerce? I'm new to nop commerce.
I added a field to ContactUs.cshtml but nothing is showing on my side when I load the form.
I have added the Postal Address but it doesn't show
<div class="fieldset">
<div class="form-fields">
<div class="inputs">
#Html.LabelFor(model => model.FullName)
#Html.TextBoxFor(model => model.FullName, new { #class = "fullname", placeholder = T("ContactUs.FullName.Hint") })
#Html.RequiredHint()
#Html.ValidationMessageFor(model => model.FullName)
</div>
<div class="inputs">
#Html.LabelFor(model => model.PostalAddress)
#Html.TextAreaFor(model => model.PostalAddress, new { #class = "postaladdress", placeholder = T("ContactUs.PostalAddress.Hint") })
#Html.RequiredHint()
#Html.ValidationMessageFor(model => model.PostalAddress)
</div>
<div class="inputs">
#Html.LabelFor(model => model.Email)
#Html.TextBoxFor(model => model.Email, new { #class = "email", placeholder = T("ContactUs.Email.Hint") })
#Html.RequiredHint()
#Html.ValidationMessageFor(model => model.Email)
</div>
#if (Model.SubjectEnabled)
{
<div class="inputs">
#Html.LabelFor(model => model.Subject)
#Html.TextBoxFor(model => model.Subject, new { #class = "subject", placeholder = T("ContactUs.Subject.Hint") })
#Html.RequiredHint()
#Html.ValidationMessageFor(model => model.Subject)
</div>
}
<div class="inputs">
#Html.LabelFor(model => model.Enquiry)
#Html.TextAreaFor(model => model.Enquiry, new { #class = "enquiry", placeholder = T("ContactUs.Enquiry.Hint") })
#Html.RequiredHint()
#Html.ValidationMessageFor(model => model.Enquiry)
</div>
#if (Model.DisplayCaptcha)
{
<div class="captcha-box">
#Html.Raw(Html.GenerateCaptcha())
</div>
}
</div>
</div>
<div class="buttons">
<input type="submit" name="send-email" class="button-1 contact-us-button" value="#T("ContactUs.Button")" />
</div>
}
}
If you are using a theme you should edit the view under the folder "Themes>NameOfYourTheme>WhatEverView" and not the one directly under the "views" folder.
Check if that is the reason.

In MVC While adding form is doing postback fine. But When Editing same from Submit button is not working. Its Not hitting Controller Method

This is my part of code . Here i can add and submit the values. But while editing submit is not working. Can anyone help me on this. What can be the Issue over here.
#using (Html.BeginForm("LenseAdd", "Stock", FormMethod.Post, new { #class = "form-horizontal form-bordered" }))
{
#Html.AntiForgeryToken()
#Html.HiddenFor(m => m.LenseId)
#Html.HiddenFor(m => m.MaterialId)
<div class="form-group">
<label class="control-label col-lg-3 col-sm-3">Lense Code<span class="text-danger">*</span></label>
<div class="col-lg-6 col-sm-8">
<div class="input-group">
#*<input type="text" class="form-control" />*#
#Html.TextBoxFor(m => m.LenseCode, new { #class = "form-control", #placeholder = "Lense Code", #id = "LenseCode" })
<span class="input-group-addon"><span class="fa fa-money"></span></span>
</div>
#Html.ValidationMessageFor(m => m.LenseCode, null, new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<label class="control-label col-lg-3 col-sm-3">BrandName<span class="text-danger">*</span></label>
<div class="col-lg-6 col-sm-8">
<div class="input-group">
#Html.DropDownListFor(m => m.BrandId, (IEnumerable<SelectListItem>)ViewBag.BrandList, "--select--", new { #class = "form-control", #id = "ddlbranchId" })
<span class="input-group-addon"><span class="fa fa-money"></span></span>
</div>
#*#Html.ValidationMessageFor(m => m.SalaryTo, null, new { #class = "text-danger" })*#
</div>
</div>
<div class="form-group">
#if (Model != null)
{
if (Model.CategoryList != null)
{
<label class="control-label col-lg-3 col-sm-3">Material</label>
foreach (var item in Model.CategoryList)
{
if (item.CategoryName == "MATERIAL")
{
<div class="col-lg-1 col-sm-3">
<div class="c-form__group -radio -inline">
<label class="c-form__control-text radio_heading">
<label>#item.Name</label>
#if (Model.MaterialId == item.ID)
{
#Html.RadioButtonFor(m => m.MaterialId, true, new { #checked = "checked", #class = "MATERIAL", #id = "MATERIAL_"+item.ID.ToString() })
}
else
{
#Html.RadioButtonFor(m => m.MaterialId, false, new { #class = "MATERIAL", #id = "MATERIAL_" + item.ID.ToString() })
}
<div class="-indicator"></div>
</label>
</div>
</div>
}
}
}
}
</div>
<div class="panel-footer">
<div class="row">
<div class="col-md-10 text-right">
<div class="btn-group">
<button type="submit" class="btn btn-custom fa fa-save -blue" >Save & New</button>
<button type="submit" class="btn btn-custom fa fa-save -skyblue" id="btn_submit">Save</button>
Cancel
</div>
</div>
</div>
</div>
}
I didn't copy the full cshtml page. here if you see any div mismatching please ignore. Any help will be appreciated.

Updating multiple list items within same view

I am trying to make a default value take application, My view loads all my value with one editor. My controller is not getting any of the data from the view?
I want to be able to edit all my value at the same time? How can I do this
Model Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Demo.Models.Admin
{
public class MerchantPackageTransactionModel
{
public MerchantPackageTransactionModel()
{
FeatureList = new List<PackageFeatureModel>();
}
public int PackageId { get; set; }
public string PackageName { get; set; }
public string Image { get; set; }
public List<PackageFeatureModel> FeatureList { get; set; }
}
public class PackageFeatureModel
{
public int FeatureId { get; set; }
public string FeatureName { get; set; }
public string Type { get; set; }
public string DefaultValue { get; set; }
public string Value { get; set; }
}
}
View Code
#model Demo.Models.Admin.MerchantPackageTransactionModel
#{
ViewBag.Title = "CreatePackageTransaction";
Layout = "~/Themes/green/Views/Shared/_AdminDashboard.cshtml";
}
#using (Html.BeginForm())
{
<fieldset>
<legend>MerchantPackageTransactionModel</legend>
#Html.HiddenFor(model => model.PackageId)
<div class="editor-label">
#Html.LabelFor(model => model.PackageName)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.PackageName)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Image)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Image)
</div>
#foreach (var item in Model.FeatureList)
{
#Html.HiddenFor(model => item.FeatureId)
<div class="editor-label">
#Html.Label("Feature Name")
</div>
<div class="editor-field">
#Html.DisplayFor(model => item.FeatureName)
</div>
<div class="editor-label">
#Html.Label("Type")
</div>
<div class="editor-field">
#Html.DisplayFor(model => item.Type)
</div>
<div class="editor-label">
#Html.Label("Default Value")
</div>
<div class="editor-field">
#Html.DisplayFor(model => item.DefaultValue)
</div>
<div class="editor-label">
#Html.Label("Value")
</div>
<div class="editor-field">
#Html.EditorFor(model => item.Value)
</div>
}
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
Controller code
public ActionResult CreatePackageTransaction(MerchantPackageTransactionModel objMerchantPackageTransactionModel)
{
foreach (var item in objMerchantPackageTransactionModel.FeatureList)
{
if (ModelState.IsValid)
{
GR.InsertOrUpdate(item);
}
}
GR.Save();
return View(objMerchantPackageTransactionModel);
}
What is happening is, because you are using
#Html.EditorFor(model => item.Value)
that is a command directly associated with the model and it is being used to render an input not accessed from the model (item), it is rendering an input html with the "wrong" name:
<input class="text-box single-line" id="item_Value" name="item.Value" type="text" value="">
when it should be
<input class="text-box single-line" id="FeatureList_0__Value" name="FeatureList[0].Value" type="text" value="">
And, as name does not match with the one from the model (FeatureList), the framework cannot do the automatic bind when the request hits the server.
You have a two options to fix it:
Create the html by hand, setting the correct (expected) name; or:
In the iteration use an index so that the EditorFor generates the input with the expected name:
#{
var index = 0;
}
#foreach (var item in Model.FeatureList)
{
#Html.HiddenFor(model => model.FeatureList[index].FeatureId)
<div class="editor-label">
#Html.Label("Feature Name")
</div>
<div class="editor-field">
#Html.DisplayFor(model => model.FeatureList[index].FeatureName)
</div>
<div class="editor-label">
#Html.Label("Type")
</div>
<div class="editor-field">
#Html.DisplayFor(model => model.FeatureList[index].Type)
</div>
<div class="editor-label">
#Html.Label("Default Value")
</div>
<div class="editor-field">
#Html.DisplayFor(model => model.FeatureList[index].DefaultValue)
</div>
<div class="editor-label">
#Html.Label("Value")
</div>
<div class="editor-field">
#Html.EditorFor(model => model.FeatureList[index].Value)
</div>
index++;
}
I have tested and it just work worked fine. Have a nice weekend! Regards.

MVC 4 - Validate only one form with multiple partial views

I'm working on a MVC 4 exercise project where I've multiple partial views and when I submit on one of this the entire page validates. I want that every button validates only their own forms.
The page looks like this:
This a partial views example:
#model Money.Models.Category
<script src="~/Scripts/jquery-1.7.1.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
#using (Html.BeginForm("Create", "Category", FormMethod.Post, new { id = "createCategoryForm" }))
{
#Html.ValidationSummary(true)
<fieldset>
<legend>Category</legend>
<div class="editor-label">
#Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Name)
#Html.ValidationMessageFor(model => model.Name)
</div>
<p>
<input id="createCategorySubmit" type="submit" value="Create" />
</p>
</fieldset>
}
And this a part of the entire view:
#model Money.Models.Transaction
#{
ViewBag.Title = "Create";
}
<h2>Create</h2>
#using (Html.BeginForm()) {
#Html.ValidationSummary(true)
<fieldset>
<legend>Transaction</legend>
<div class="editor-label">
#Html.LabelFor(model => model.CategoryId, "Category")
</div>
<div class="editor-field">
#Html.DropDownList("CategoryId", String.Empty)
#Html.ValidationMessageFor(model => model.CategoryId)
</div>
<div id="createCategory">
#Html.Partial("~/Views/Shared/_CreateCategory.cshtml")
</div>
<div class="editor-label">
#Html.LabelFor(model => model.AccountId, "Accout")
</div>
<div class="editor-field">
#Html.DropDownList("AccountId", String.Empty)
#Html.ValidationMessageFor(model => model.AccountId)
</div>
<div id="createAccount">
#Html.Partial("~/Views/Shared/_CreateAccount.cshtml")
</div>
Have you some idea?
Thank you
You have to parse the new content with the validator:
$('form').data('validator', null);
$.validator.unobtrusive.parse($('form'));