开发者

ASP.NET MVC Validation

开发者 https://www.devze.com 2023-02-03 01:46 出处:网络
I have 2 scenarios that I need 开发者_运维百科some help with re validation in my ASP.NET MVC application. I\'m aware that having validation within the controller is not ideal, so am looking to keep th

I have 2 scenarios that I need 开发者_运维百科some help with re validation in my ASP.NET MVC application. I'm aware that having validation within the controller is not ideal, so am looking to keep this elsewhere - perhaps with my models where I can.

1) I have a model with various properties, some of which have validation against them using DataAnnotations. I'm then using the Html helper methods within my view to expose any validation errors against the relevant fields. For the most part, these work as expected. The exception I've come up against is where one of the fields in my view is a dropdown list. The first item within my list is empty/blank, the rest are genuine values. The property in my model that this field relates to has the following against it:

[Required(ErrorMessage = "A value from the list is required")]

At present, if I leave the default value in the list (blank) and don't select a genuine value from the list, I want it to render the validation error message, but it's currently treating it as if it were a valid value, and passing that validation.

How can I get it to fail validation if that blank/empty list item is submitted?

2) On one of my views, I have a few file upload controls, enabling the user to upload images to the website. These fields are not directly bound to any properties within my model - only the resulting filename's (once the file has been uploaded, converted, renamed etc.) are then assigned to 'Filename1', 'Filename2' etc. properties within my model.

So, I am wondering how to best go about validating that these mandatory file uploads? At present I am doing the following for each of the file upload controls, within my controller(!):

HttpPostedFileBase file = null;

file = Request.Files["Filename1"];
        if (file != null && file.ContentLength == 0)
                ModelState.AddModelError("Filename1", "Image1 is required");

Once this is done for each of the file upload controls, I check if the ModelState is valid:

if (ModelState.IsValid)

I'm sure there must be a better way of performing this validation, and I'd imagine it's not ideal to have this in the controller, but I'm not sure the best way to handle this.

I'd appreciate any help with these 2 scenarios.


Here is my code (explanations later) :

The form:

<% using (Html.BeginForm("TestForm", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
   { %>
   <%: Html.DropDownList("ComboboxValue", new SelectList(Model.ComboboxValues)) %><br />
   <input type="file" id="FileUpload" name="FileUpload" /><br />
   <input type="submit" id="submit" name="submit" value="Valider" />
<%} %>

The model:

public class TestFormModel
{
    [Required(ErrorMessage = "A value from the list is required")]
    public string ComboboxValue { get; set; }

    public List<string> ComboboxValues { get; set; }

    public HttpPostedFileBase FileUpload { get; set; }

    public ModelStateDictionary IsFileValid()
    {
        ModelStateDictionary modelState = new ModelStateDictionary();
        modelState.AddModelError("FileUpload", "Here is the problem.");
        return modelState;
    }

The controller :

    public ActionResult TestForm()
    {
        TestFormModel model = new TestFormModel();
        model.ComboboxValues = new List<string>(){
            "", "Red", "Blue", "Yellow"
        };
        return View("TestForm", model);
    }
    [HttpPost]
    public ActionResult TestForm(TestFormModel model)
    {
        model.ComboboxValues = new List<string>(){
            "", "Red", "Blue", "Yellow"
        };
        ModelState.Merge(model.IsFileValid());
        return View("TestForm", model);
    }

1) If you have a null value, your model wont be valid.

2) To avoid Request.Files["Filename1"]; you can "type" your form (new { enctype = "multipart/form-data" }). With this, your model will contain the file. You can add an extension method, for example :

public static bool IsCSVValid(this HttpPostedFileBase file)
{
    return (file != null && file.ContentLength != 0 && file.FileName.EndsWith(".CSV", StringComparison.InvariantCultureIgnoreCase));
}

And you can add error from somewhere with ModelState.Merge().

I dont know if it's a "good thing to do", but it works pretty well :)


For #1, what is the value that is getting posted for the dropdown list and what is the type in your model that it is trying to bind against? If it is posting a value like " ", then it will pass validation. You need to make sure it is an empty string if your type is a string.

For #2, you could try writing a custom model binder, but not sure how ugly that would be to get access to the files. Another option is to make it part of your parameters on the action result:

public ActionResult Test(TestModel model, HttpPostedFileBase files)

This will at least do the auto binding for you if a file exists, but you would still have to perform the manual validation like you would do before. I know its not the exact answer you are looking for, but it does clean up the file code a little bit more.

Also, this question might help a little more: ASP.NET MVC posted file model binding when parameter is Model


It seam like you are working on ASP.net Dynamic data. If yes, ASP.Net Dynamic data work on the concept Module view control.

In MVC Loading a control for a column depend on the metadata of a class. It read the data type of the class and then load control accordingly. If you make any changes to these control will affect other places as will.

Top handle this you should create new control and do the validation in that control and in class for that column you can specify that it should use your customized control.

Please let me know if you need more details on how to customize control.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号