What is the recommended way to collect input values ​​in an ASP.NET web form

I am trying to develop an ASP.NET book-by-book application; I have web forms that implement the MVP template, my host talks only to the Services level, than talks to a bunch of repositories, everyone uses my own EF ObjectContext. So far, so good. Now I'm dealing with the front end, and I have a dilemma ...

Suppose I have an asp: TextBox control named txtDateOfBirth and a submit button. When the user clicks the button, I need to send data from ASPX to the service leading .... but the problem is that in the end I don't need String, but DateTime? and I don't know a better place to convert :

  • Should I put the validator on an ASPX page and then do the conversion to code?
  • Or do I need to collect a bunch of lines from a form, create a request for a service containing only lines, and how will my business model make try / convert and report errors?

What do you recommend? Any insight is greatly appreciated ...

EDIT: OK, after reading and trying all your suggestions, I decided to go with the following:

  • Responses for data type conversion are responsible for code and APSX. I decided to go with a simple CompareValidator and check the appropriate type, so the code-code can undoubtedly convert it.
  • The request that goes from the master to the service level corresponds to the dialed.
  • All other checks are performed by the business layer (including the length of the string, required or not, range, ...)
+4
source share
3 answers

If I understand correctly, you have a text box in your WebForm where the user writes something that should be a date, right?

There is something you can do:

+1
source

I would say that for the presentation layer it is legitimate to convert primitive types to any business layer that you need to deal with.

For example, in an ASP.NET MVC environment (I know you don’t use it), the business layer interacts with the presentation layer using “models”, which are simple business-specific .NET objects designed to connect three MVC layers.

+4
source

1) Code code must do the conversion

  • For example, an input line (from txtDateOfBirth) to DateTime or DateTime?

2) Business logic must verify business rules

  • For example, the user must be at least 16 years old

3) The method should be

  • InsertUser(User user) {} or
  • InsertUser(string firstName, string lastName, DateTime or DateTime? dateOfBirth) {}

You can see that the .Net Framework uses mostly strong type parameters instead of a string and an object.

If you do not want to pass values ​​with a null value, you can use the following approach, which is used in DotNetNuke .

 public class Null { public static int NullInteger { get { return -1; } } public static decimal NullDecimal { get { return decimal.MinValue; } } public static DateTime NullDate { get { return DateTime.MinValue; } } ... } 

Edited . At your request, I added code by code. This is not really a template; it's just a person’s preference.

 public string FirstName { get { return FirstNameTextBox.Text; } } public string LastName { get { return LastNameTextBox.Text; } } public DateTime DateOfBirth { get { DateTime d; return DateTime.TryParse(DateOfBirthTextBox.Text, out d) ? d : Null.NullDate; } } protected void SaveButton_Click(object sender, EventArgs e) { try { var user = new Users() { FirstName = this.FirstName, LastName = this.LastName, DateOfBirth = this.DateOfBirth }; UserService.InsertUser(user); ... } Catch (Exception ex) { // Log error } } 
+1
source

Source: https://habr.com/ru/post/1441816/


All Articles