As the title, i using remote validation to valid the Username field. But the name of the field in Html is User.Username. How can i pass the parameter with the name having dot User dot Username. If dont use the parameter with the match field name. The validation not works? Anyone can give a solution?
UPDATE SOURCE CODE:
Here is my action method:
public JsonResult ValidateUsername(string Username)
{
var user = IUserRepo.GetUserByUrName(Username);
if (user!=null)
{
return Json("Vui lòng chọn tên khác, tên đăng nhập đã tồn tại.",JsonRequestBehavior.AllowGet);
}
return Json(true,JsonRequestBehavior.AllowGet);
}
And the property to valid in Entities:
[Required(ErrorMessage="Tên đăng nhập không được rỗng.")]
[StringLength(10,ErrorMessage="Tên đăng nhập không được vượt quá 10 ký tự.")]
[RegularExpression(#"(\S)+", ErrorMessage = "Không được có khoảng trắng.")]
[Remote("ValidateUsername", "Account")]
public string Username { get; set; }
And the view:
<tr>
<td class="info_label">Tên đăng nhập</td>
<td>#Html.EditorFor(m => m.User.Username)
</td>
<td class="check_user">#Html.ValidationMessageFor(m => m.User.Username)</td>
</tr>
The name of the view in firebug is User.Username but the parameter pass to action method is Username. It doesn't match so it's is the reason the field not invoke or valid. How can is pass User.Username as parameter?
You have to just use the model binding capability of MVC, just having the form like
<input type='text' name='User.Username' value='' />
<input type='password' name='User.Password' value='' />
You can have the logon method the account or whatsoever other controller you have to look like this
public ActionResult Logon(User userDetails){
if(Validate(userDetails)){
// allow the access to the app
}
else {
//return the logon view with the appropriate model state error messages
}
}
In case you need to post the data via Ajax to the action method, you can use like the following
function ValidateUserName(){
var user = {};
user.UserName=$("#userName").val();
$.ajax({
// other defaults here
data: JSON.stringify(user);
contentType:"application/json",
// other defaults here
});
}
Related
I have searched for this, and found several solutions, but none seem to work for me.
I'm using Razor to build an MVC view.
The model I'm passing in is defined as follows:
public class ActiveProxiesDTO
{
/// <summary> error message returned by the transaction </summary>
public string ErrorMsg { get; set; }
/// <summary> List of proxies returned by the transaction </summary>
public List<ActiveProxiesDetailDTO> ActiveProxies { get; set; }
}
/// <summary> A single proxy </summary>
public class ActiveProxiesDetailDTO
{
/// <summary> Proxie's PERSON id </summary>
public string ProxyId { get; set; }
/// <summary> Proxie's First Name </summary>
public string FirstName { get; set; }
/// <summary> Proxie's Last Name </summary>
public string LastName { get; set; }
/// <summary> Proxie's email address </summary>
public string EmailAddress { get; set; }
/// <summary> Does user want to reset this proxy's password?</summary>
public bool Reset { get; set; }
}
In the action method that invokes the view I set the "Reset" bool to false for all items in the list.
My view is defined as follows:
#model Ellucian.Colleague.Dtos.Base.M32.ActiveProxiesDTO
#using (Html.BeginForm("PostResetProxy", "Core", FormMethod.Post, new { Area = "M32" }))
{
#Html.AntiForgeryToken()
<div class="dataentry">
Proxies:
<br />
<table>
<tr>
<th>Reset?</th> <th>Name</th> <th>Email Address(to send new password)</th>
</tr>
#for (int i = 0; i < Model.ActiveProxies.Count; i++)
{
<tr>
<td>
<!-- make sure all data bind to the model being passed to the next controller action method -->
<input type="hidden" name="ActiveProxies[#i].ProxyId" value="#Model.ActiveProxies[i].ProxyId" />
<input type="hidden" name="ActiveProxies[#i].FirstName" value="#Model.ActiveProxies[i].FirstName" />
<input type="hidden" name="ActiveProxies[#i].LastName" value="#Model.ActiveProxies[i].LastName" />
<!-- checkboxes require both a hidden and non-hidden input element -->
<input type="hidden" name="ActiveProxies[#i].Reset" value="#Model.ActiveProxies[i].Reset" />
<input type="checkbox" name="ActiveProxies[#i].Reset" value="true" />
</td>
<td>
#{ var displayName = Model.ActiveProxies[i].FirstName + " " + Model.ActiveProxies[i].LastName; }
#displayName
</td>
<td>
<input type="text" name="ActiveProxies[#i].EmailAddress" value="#Model.ActiveProxies[i].EmailAddress" />
</td>
</tr>
}
</table>
<br />
<input type="submit" />
...
for now, the action method handling the POST is just:
[HttpPost]
public ActionResult PostResetProxy( ActiveProxiesDTO formData )
{
return Redirect(Url.Action("Index", "Home", new { Area = "" }));
}
I set a breakpoint at the return statement and check the contents of formData.
Everything looks correct: The entire ActiveProxies list is present, If I enter email addresses on the form they post correctly to the proper entry of the list.
The problem is that, whether I check the "reset" box or not, the value is always passed back as false.
I've tried using Html.CheckBox and Html.CheckBoxFor in various ways as described elsewhere in this forum, but with no success. I've tried using string instead of bool for the checkbox value, but that also failed. I'm out of ideas. Anyone see what I'm doing wrong? -- TIA Gus
I found a solution that works for me (it does not involve JavaScript (I'm not yet proficient in it).
Upon return to the method handling the Post request I checked the values in Request.Form[ "ActiveProxies[i].Reset"] and noticed that the value was always ",true" (strange that it has a "," prefix! A bug?) for every row of the table where I had "Reset?" checked, and the empty string wherever I had it unchecked.
So I got around the issue by adding logic at the top of my POST action that corrects the values in my formData argument. The (unfinished) Post method now reads as follows
[HttpPost]
public ActionResult PostResetProxy( ActiveProxiesDTO formData )
{
for ( int i=0 ; i < formData.ActiveProxies.Count ; i++ )
{
string x = Request.Form[ "ActiveProxies["+i+"].Reset"];
formData.ActiveProxies[i].Reset = x.Contains("true") ? true : false;
}
// for the time being, just return to the home page
return Redirect(Url.Action("Index", "Home", new { Area = "" }));
}
I've just discovered a better solution.
I had originally tried the #Html.CheckBox and #Html.CheckBoxFor helpers without success and therefore gave up on using helpers for my checkbox input.
To my surprise, the more general purpose "EditorFor" helper turned out to work where the ones purported to be specifically for a checkbox did not.
I replaced my original lines
<!-- checkboxes require both a hidden and non-hidden input element -->
<input type="hidden" name="ActiveProxies[#i].Reset" value="#Model.ActiveProxies[i].Reset" />
<input type="checkbox" name="ActiveProxies[#i].Reset" value="true" />
with the single line
#Html.EditorFor(m => Model.ActiveProxies[i].Reset)
and ... to my surprise, it worked, allowing me to take the kluge code out of my POST method.
I searched around on Stack Overflow, but could not find the solution to my query. I have a controller function that adds multiple model attributes on a GET request
#RequestMapping(method = RequestMethod.GET, value = "/showdeletesearchqueryform")
public String showDeleteSearchQuery(final Model model) {
if (LOG.isDebugEnabled()) {
LOG.debug("Fetching all the search query results.");
}
ImmutableList<ArtQueryResults> results = this.searchQueriesService
.getSearchQueries(APPNAME);
// Adding model attribute # 1
model.addAttribute("searchResults", results);
if (LOG.isDebugEnabled()) {
LOG.debug("\"searchResults\" model attribute has been intialized from "
+ results);
}
ArtDeleteQueryRequest request = new ArtDeleteQueryRequest();
request.setAppName(APPNAME);
if (LOG.isDebugEnabled()) {
LOG.debug("Model attribute initialized = " + request);
}
// Adding model attribute # 2
model.addAttribute("deletedAttributes", request);
return "deletesearchqueries";
}
My JSP
<div class="column-group">
<form:form method="POST" action="${pageContext.request.contextPath}/arttestresults/showdeletesearchqueryform" modelAttribute="deletedAttributes">
<form:errors path="*" cssClass="alert alert-danger column lg-units-5 units-2" element="div"/>
<form:hidden path="appName" id="appNameId" htmlEscape="true"/>
<div class = "units-1 column lg-units-12">
<!-- Hidden Key for app name. -->
<form:select path="idsToBeDeleted" id="IdsToBeDeletedSelectId">
<c:forEach items="${searchResults}" var="searchResult" varStatus="loop">
<form:option label="${searchResult.searchQuery}" value="${searchResult.id}" />
</c:forEach>
</form:select>
</div>
<div class="units-1 column lg-units-12">
<%-- This is a hack that make sure that form is submitted on a click. Not sure why form is not being submitted. --%>
<button class="button" type="submit" onclick="javascript:$('form').submit();">Delete Selected Queries</button>
</div>
</form:form>
My controller POST function
#RequestMapping(method = RequestMethod.POST, value = "/showdeletesearchqueryform")
public String deleteSearchQueries(
Model model,
#ModelAttribute(value = "deletedAttributes") #Valid final ArtDeleteQueryRequest request,
final BindingResult result) {
if (result.hasErrors()) {
LOG.warn("There are " + result.getErrorCount() + " validation errors.");
return "deletesearchqueries";
} else {
if (LOG.isDebugEnabled()) {
LOG.debug("The ids to be deleted are " + request.getIdsToBeDeleted());
}
this.searchQueriesService.deleteSearchQueriesById(
ImmutableList.copyOf(request.getIdsToBeDeleted()));
return "redirect:/arttestresults/showdeletesearchqueryform";
}
}
If there is a validation failure, the model attribute searchResults is not being picked up when I return a view on error condition? Is there a way to preserve the other defined model attributes as well?
Seems that you need flash attributes which were added in spring 3.1. Please take a look at example/explanation:
http://viralpatel.net/blogs/spring-mvc-flash-attribute-example/
The get and the post are different requests. What you get in the post request, is only what comes from the form, so only the "deletedAttributes" model attribute and only the fields that are <input> in the JSP.
You need to put again the searchResults model attribute explicitely like you did in get method.
As suggested by M. Deinum, if one or more attribute(s) will be used by all methods in a controller, you can use a #ModelAttribute annotated method to put it (them) in model automatically.
You can also use SessionAttributes model attributes, that is attributes that are stored in session and not in request. But it is hard to have them properly cleaned from session if user do not post the form but go into another part of the application. You have an example of usage ofSessionAttributes` in Spring's Petclinic example.
In an MVC3 project, i use an Html.BeginForm to post some (model-)values. Along with those i want to send an extra parameter that is not part of the form (the model) but in the ViewBag. Now, when i use a Button (code in answer here: MVC3 razor Error in creating HtmlButtonExtension), all the form values are posted but the extra parameter remains null. When i use an ActionLink, the parameter is posted but the form values are not :) Any know how i can combine the two? Thanks!
#Html.Button("Generate!", new { id = ViewBag.ProjectID })
#Html.ActionLink("Generate!", "Post", new { id = #ViewBag.ProjectID })
My advice would be to declare a new Object in your App.Domain.Model something like this
namespace App.Domain.Model
{
public class CustomEntity
{
public Project projectEntity { get; set; }
public int variableUsed { get; set; }
}
}
In your view you can acces them easily by using CustomEntity.projectEntity and CustomEntity.variableUsed.
Hope it helps
You can do something like below.
View code
#using (Html.BeginForm("ActionName", "ControllerName", FormMethod.Post, new { #id = "frmId", #name = "frmId" }))
{
#*You have to define input as a type button not as a sumit. you also need to define hidden variable for the extra value.*#
<input type="hidden" name="hndExtraParameter" id="hndExtraParameter" />
<input value="Submit" type="button" id="btnSubmit" onclick="UpdateHiddenValue()" />
}
<script type="text/javascript">
function ValidateUser() {
$("#hndExtraParameter").val('Assignvaluehere');
$("#frmId").submit();
}
</script>
Controller Code
[HttpPost]
public ActionResult ActionName(Model model, string hndExtraParameter)
{
//Do your operation here.
}
I have created one page in MVC 3.0 Razor view.
Create.cshtml
#model LiveTest.Business.Models.QuestionsModel
#*<script src="#Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="#Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>*#
#using (Html.BeginForm())
{
#Html.ValidationSummary(true)
<table cellpadding="1" cellspacing="1" border="0">
<tr>
<td>#Html.LabelFor(model => model.TestID)
</td>
<td>
#Html.DropDownListFor(model => model.TestID, (IEnumerable<SelectListItem>)ViewBag.ItemIDList)#Html.ValidationMessageFor(model => model.TestID)
</td>
</tr>
<tr>
<td>#Html.LabelFor(model => model.Question)
</td>
<td>#Html.EditorFor(model => model.Question)#Html.ValidationMessageFor(model => model.Question)
#Html.HiddenFor(model => model.QuestionsID)
</td>
</tr>
<tr>
<td>#Html.LabelFor(model => model.IsRequired)
</td>
<td>#Html.CheckBoxFor(model => model.IsRequired)#Html.ValidationMessageFor(model => model.IsRequired)
</td>
</tr>
<tr>
<td>
</td>
<td>
<input type="submit" value="Submit" />
</td>
</tr>
</table>
}
QuestionsController.cs
public class QuestionsController : Controller
{
#region "Attributes"
private IQuestionsService _questionsService;
#endregion
#region "Constructors"
public QuestionsController()
: this(new QuestionsService())
{
}
public QuestionsController(IQuestionsService interviewTestsService)
{
_questionsService = interviewTestsService;
}
#endregion
#region "Action Methods"
public ActionResult Index()
{
return View();
}
public ActionResult Create()
{
InterviewTestsService _interviewService = new InterviewTestsService();
List<InterviewTestsModel> testlist = (List<InterviewTestsModel>)_interviewService.GetAll();
ViewBag.ItemIDList = testlist.Select(i => new SelectListItem() { Value = i.TestID.ToString(), Text = i.Name });
return View();
}
[HttpPost]
public ActionResult Create(QuestionsModel questions)
{
if (ModelState.IsValid)
{
_questionsService.Add(questions);
return RedirectToAction("Index");
}
InterviewTestsService _interviewService = new InterviewTestsService();
List<InterviewTestsModel> testlist = (List<InterviewTestsModel>)_interviewService.GetAll();
ViewBag.ItemIDList = testlist.Select(i => new SelectListItem() { Value = i.TestID.ToString(), Text = i.Name });
return View(questions);
}
#endregion
}
QuestionsModel.cs
public class QuestionsModel : IQuestionsModel
{
[ReadOnly(true)]
public Guid QuestionsID { get; set; }
[Required]
[DisplayName("Question")]
public string Question { get; set; }
[DisplayName("Test ID")]
public Guid TestID { get; set; }
[DisplayName("Is Required")]
public bool IsRequired { get; set; }
[DisplayName("Created By")]
public Guid CreatedBy { get; set; }
}
Problem:
<script src="#Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="#Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
If I am adding the above two lines in Create.cshtml page and then I press submit button then it will fire validation message "Question is required!" if I am entering value in *Question field and then press submit button my [HttpPost]Create Method never execute.*
If I remove the above two lines from page then press submit button then it will execute [HttpPost]Create Method and fire validation from server side if I am entering value in Question field then also [HttpPost]Create executed.
Please help me.
The QuestionsModel class includes a property CreatedBy which is not included in your View.
Try either adding CreatedBy as a hidden field, or (better yet) remove CreatedBy from the QuestionsModel class, since it is not an attribute which should be exposed in the view.
I suspect that this missing property is the cause of the problem.
UPDATE
I ran some tests on your code, and it was not the CreatedBy property. Rather, your problem is that you are not supplying a QuestionsID value, but you included a hidden field for QuestionsID on the form.
Because QuestionsID is a value type, by default, the DataAnnotationsModelValidatorProvider adds a Required validator to the QuestionsID field. Because the field did not have a ValidationMessage, you could not see the validation error.
You can override the behavior of the default DataAnnotationsModelValidatorProvider by following the instructions in my answer here.
I would check if any client side errors occurred when trying to submit the form. Check it from the browser console.
Also, make sure that you have completed your code with no validation errors before submitting the form.
Are you saying that the form doesn't validate client side and nothing ever get's POSTed back to your server?
Meaning, you click the submit button and nothing happens in the browser, correct?
The problem might be that your form isn't validating with the unobtrusive javascript library validation.
i have designed a view in asp .net mvc3 off course registration form. This is very simple form having name ,father name , qualification and a submit button , after pressing submit button i want to display information by using another view. please suggest me how can i send information from one view to another view.
my controller class is :
namespace RegistrationForm.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
// ViewBag.Message = "Welcome to ASP.NET MVC!";
//return View();
return RedirectToAction("registrationView");
}
public ActionResult About()
{
return View();
}
public ActionResult registrationView()
{
return View();
}
}
}
my view is :
#{
Layout = null;
}
registrationView
Enter Name
</td>
<tr>
<td>
Enter Father Name
</td>
<td>
<input type="text" name="fname" id="fname" />
</td>
<tr>
<td>
Enter Qualification
</td>
<td>
<input type="text" name="qly" id="qly" />
</td>
</tr>
</table>
<input type="submit" value="submit" />
</div>
well, we faced this problem before, and the best way to get this to work was to define a model that this page will work with, then use this model object when posting back, or redirecting to another view.
for your case, you can simply define this model in your Models folder
ex: RegistrationModel.cs file, and define your required properties inside.
after doing so, you will need to do 2 more steps:
1- in your GET action method, create a new RegistrationModel object, and provide it to your view, so instead of:
return View();
you will need something like:
var registrationModel = new registrationModel();
return View(registrationModel);
2- Use this model as a parameter in your POST Action method, something like
[HttpPost]
public ActionResult registrationView(RegistrationModel model)
{
// your code goes here
}
but don't forget to modify the current view to make use of the provided model. a time-saver way would be to create a new dummy View, and use the pre-defined template "Create" to generate your View, MVC will generate the properties with everything hooked up. then copy the generated code into your desired view, and omit any unneeded code.
this is a Pseudo reply. if you need more code, let me know
<% using Html.Form("<ActionName>") { %>
// utilize this HtmlHelper action to redirect this form to a different Action other than controller that called it.
<% } %>
use ViewData to store the value.
just remember that it will only last per one trip so if you try to call it again, the value would have been cleared.
namespace RegistrationForm.Controllers { public class HomeController : Controller { public ActionResult Index() { // ViewBag.Message = "Welcome to ASP.NET MVC!";
ViewData["myData"] = "hello world";
//return View();
return RedirectToAction("registrationView");
}
public ActionResult About()
{
return View();
}
public ActionResult registrationView()
{
// get back my data
string data = ViewData["myData"] != null ? ViewData["myData"].ToString() : "";
return View();
}
}
And you can actually usethe ViewData value on the html/aspx/ascx after redirect to the registrationView.
For example on the registrationView.aspx:
<div id="myDiv">
my data was: <%= ViewData["myData"] %>
</div>
You could simply in you method parameter list declare the parameters with the name of the controls. For example:
The control here has an id "qly"
<input type="text" name="qly" id="qly" />
Define your method parameter list as following:
public ActionResult YourMethod(string qly)
{
//simply pass your qly to another view using ViewData, TempData, or ViewBag, and use it in the desired view
}
You should use TempData which was made exactly for it, to persist values between actions.
This example is from MSDN (link above):
public ActionResult InsertCustomer(string firstName, string lastName)
{
// Check for input errors.
if (String.IsNullOrEmpty(firstName) ||
String.IsNullOrEmpty(lastName))
{
InsertError error = new InsertError();
error.ErrorMessage = "Both names are required.";
error.OriginalFirstName = firstName;
error.OriginalLastName = lastName;
TempData["error"] = error; // sending data to the other action
return RedirectToAction("NewCustomer");
}
// No errors
// ...
return View();
}
And to send data to the view you can use the model or the ViewBag.