modelstate

how to validate in modal

家住魔仙堡 提交于 2021-01-28 07:30:35
问题 I have a modal that returns login view . I want to check if user does not exist return the view with some error . I tried using ModelState.AddModelError() But the modal close and view is opened. this is my code: public IActionResult Login(LoginViewModel login) { if (!ModelState.IsValid) return View(); var user = _userServies.getUserByEmailandPass(login.Email, login.Password); if (user == null) { ModelState.AddModelError("Email","email or password is wrong"); return view(); } return Redirect("

ModelState.AddModelError - no error shown

流过昼夜 提交于 2020-01-24 06:11:45
问题 I'd like to set an error message for an email field after an exception has been caught in my controller: ... catch (EmailAlreadyExistsException emailAlreadyExistsException) { ModelState.AddModelError("Useraccount_Email", "This is an error message"); RegisterViewModel viewModel = new RegisterViewModel { Useraccount = useraccount, InformationMessages = InformationMessageHelper.GetInformationMessages() }; return PartialView(viewModel); } ... The error message should now be displayed in my view:

ModelState.AddModelError - no error shown

僤鯓⒐⒋嵵緔 提交于 2020-01-24 06:07:08
问题 I'd like to set an error message for an email field after an exception has been caught in my controller: ... catch (EmailAlreadyExistsException emailAlreadyExistsException) { ModelState.AddModelError("Useraccount_Email", "This is an error message"); RegisterViewModel viewModel = new RegisterViewModel { Useraccount = useraccount, InformationMessages = InformationMessageHelper.GetInformationMessages() }; return PartialView(viewModel); } ... The error message should now be displayed in my view:

View the Validation summery in the required place

淺唱寂寞╮ 提交于 2020-01-16 00:27:06
问题 I have a partial view in a main view in mvc3. The partial view has it's own ModelState.AddModelError in its action method and the main view has another ModelState.AddModelError in its action method. When the code runs and the main view error wants to be displayed, as the partial view also has the validationsummery tag, the validation summery shows up in both places. How an I fix this issu?? Thanks. 回答1: You can do this by jquery $(function(){ var error = ""; $("classnameOfvalidationsummarydiv

ModelState always valid

▼魔方 西西 提交于 2020-01-04 05:31:31
问题 I've got something seemingly very simple not working. I have got a model public class Name: Entity { [StringLength(10), Required] public virtual string Title { get; set; } } public class Customer: Entity { public virtual Name Name { get; set; } } a view model public class CustomerViweModel { public Customer Customer { get; set; } } a view <% using(Html.BeginForm()) { %> <%= Html.LabelFor(m => m.Customer.Name.Title)%> <%= Html.TextBoxFor(m => m.Customer.Name.Title)%> <button type="submit"

ASP.NET MVC Model State

怎甘沉沦 提交于 2020-01-04 02:20:08
问题 ModelState.IsValid is returning false for me in my controller. I know that means one or more model errors were found when model binding. My question is how do I see the errors? I noticed that my particular ModelState has 6 items in it. If I try to do any of these... ModelState[0].Errors[0].ToString() ModelState[0].Errors[0].ErrorMessage ModelState[0].Value.AttemptedValue I get this error: The best overloaded method match for 'System.Web.Mvc.ModelStateDictionary.this[string]' has some invalid

ModelState.IsValid is false prior to validation

怎甘沉沦 提交于 2019-12-31 06:58:40
问题 We wrote a custom model binder that overrides the CreateModel method of the ComplexTypeModelBinder so that we can have injection into our ViewModels instead of having to pass the injected clients and repos to our model from the controller . For example, for a model like this: public class ThingViewModel { public ThingViewModel (IThingRepo thingRepo) {} } In our controller we can do: public class ThingController : Controller { public IActionResult Index(ThingViewModel model) => View(model); }

Extending ModelState to output my own message - how do I get model state value

落爺英雄遲暮 提交于 2019-12-24 19:40:23
问题 I wrote my own extension for ModelStateDictionary, but I am running in to the problem of displaying the inputted value. Here is my attempt: public static string ToLoggingFormat(this ModelStateDictionary modelState) { var sb = new StringBuilder(); sb.AppendLine("Input validation error occurred:"); foreach (var ms in modelState.Values) { foreach (ModelError error in ms.Errors) { sb.AppendLine(error.ErrorMessage); } if (!string.IsNullOrEmpty(ms.AttemptedValue)) { sb.AppendLine($"Attempted Value:

ASP.NET MVC: DataAnnotations - Show an error message indicating that a field must be numeric

匆匆过客 提交于 2019-12-24 00:53:05
问题 There appears to be something of a hole in the way DataAnnotations works in that a user entering in some text into a field that will go into an int will never reach the DataAnnotations code. It kicks off a model binding error and displays the error to the user "The value 'a' is not valid for the XXXX field." Anyway, it's all very nice that it automatically handles this situation, but I actually want to display an error message indicating the problem eg. "The value 'a' is not numeric. Please

ASP.NET MVC 2 Model Errors with Custom Exceptions

*爱你&永不变心* 提交于 2019-12-24 00:34:22
问题 I have a custom exception class: public class MyException: Exception { public MyException(MyExceptionEnum myError) : base(myError.ToDescription()) { } public MyException(MyExceptionEnum myError, Exception innerException) : base(myError.ToDescription(), innerException) { } } .ToDescription is a extension method on MyExceptionEnum to provide enum-to-string mapping for the exception error details. Here's how i throw it: if (someCondition) throw new MyException(MyExceptionEnum.SomeError); So i