I can not get the value in the text box that I placed in the controller

I am using Asp.net MVC 4 and .NET 4.5. I have a single column table that is a decimal, not a null value. I created a razor view using the MVC forest pattern for the model created by the Entity framework for this table.

Now, when we enter 0 or nothing (null) in the text field of the decimal property, on the server it comes as 0. and after checking it is displayed as zero in the text field.

Is there any way by which we can determine if the client is entered in the zero or zero value in the text field, so that after sending the message back, if there is any check, the client receives the value that he published

Update 1

public partial class Student
{
    public int StudentID { get; set; }
    public string LastName { get; set; }
    public string FirstMidName { get; set; }
    public System.DateTime EnrollmentDate { get; set; }
    public decimal RollNo { get; set; }
}

- class generated by EF.

@Html.TextBox("Students[0].RollNo", Model.Students[0].RollNo)

, .

+3
4

, : ASP.NET MVC:

public class MyAttribute : ValidationAttribute
{
    public override bool IsValid(object value)
    {
        int temp;

        if (!Int32.TryParse(value.ToString(), out temp))
            return false;

        return true;
    }
}

[MyAttribute]

EDIT:

, double?. , .

public partial class Student
{
    public int StudentID { get; set; }
    public string LastName { get; set; }
    public string FirstMidName { get; set; }
    public System.DateTime EnrollmentDate { get; set; }
    public decimal? RollNo { get; set; }
}

MSDN

2:

viewmodels backupproperties, : .

public class DoubleModelBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var valueResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
        if (string.IsNullOrEmpty(valueResult.AttemptedValue))
        {
            return double.NaN;
        }
        return valueResult;
    }
}

 protected void Application_Start()
 {
     ModelBinders.Binders.Add(typeof(double), new DoubleModelBinder());
 }

double.NaN , . , .

+3

decimal not null value [...] - , , , , - , , /

, ViewModel , . Entity Framework .

+3

[Required] [DisplayFormat] , null .

public partial class Student
{
    public int StudentID { get; set; }
    public string LastName { get; set; }
    public string FirstMidName { get; set; }
    public System.DateTime EnrollmentDate { get; set; }
    [Required]
    [DisplayFormat(DataFormatString = "{0:0.###}")]
    public decimal RollNo { get; set; }
}
-1

:

<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>

:

@using (Ajax.BeginForm("Action", "Controller",null, new AjaxOptions{})
{
   @for(int i=0;i<Model.Students.Count;i++)
   {
       @Html.TextBoxFor(c=>Model.Students[i].RollNo)
   }
       <input type="submit" value ="submit"/>
}

, , MIN, MAX .. ClientValidationEnabled=true - Layout jquery.unobtrusive-ajax.js. , !

, DataAnnotationsExtensions.   Ad . , .

Note: * Min, Max are not included by default, you can use them with DataAnnotationsExtensions. *

-1
source

All Articles