1

I've been looking around stackoverflow for this answer. Can't seem to get anywhere with them. I have an entity which has 5 navigation properties to other entities. Example:

public class Computer : DbEntity
{
    public virtual Ram Ram { get; set; }
    public Guid RamId { get; set; }

    public virtual Hdd Hdd { get; set; }
    public Guid HddId { get; set; }

    // etc. etc. you get the picture

}

In order to build the View up, I made partial Create views for Hdd and Ram etc. and referenced them into the main view:

@model ComputerSite.ViewModels.ComputerViewModel

@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Computer Details</legend>
        ... some fields ...
    </fieldset>

    Html.RenderPartial("Create", "Ram");

    Html.RenderPartial("Create", "Hdd");
}

And I tested this and it works, all the data gets posted back to the ViewModel and it can be saved in the database, so I know all of that works. The problem comes when, a Computer has no Ram. I want to be able to check a box to say this won't be included. And when the form is submitted to bypass the Ram validation.

There should be a very easy and simple way, I don't mind if it is a JavaScript function that goes and changes some data- attributes client side. I'm just not entirely sure where to start, custom attributes aren't the way forward with this. It seems way to much just to change some fields. I still want the fields validated if the check box isn't checked and the validation is in place for that, and that bit works, hence why I'm asking the question, I just need to be able to selectively turn off different parts of the form.

Oh, and as a side note, NON of my properties in any of my entity classes have the [Required] attribute!

Examples please if you do JavaScript. I'm 100% proficient with it yet.

3
  • Are you open to JS solutions only? Commented May 17, 2013 at 10:08
  • well I'm using jquery unobtrusive validation, so it would have to be something that would tie into that, I can't imagine I need to do anything server side. Just disable a few fields using JS, what is your suggestion? Commented May 17, 2013 at 10:12
  • I use fluentvalidation.codeplex.com very often. It supports only simple validations on client-side, but it is very powerful for server-side Commented May 17, 2013 at 10:20

2 Answers 2

1

Take a look at the foolproof validation project, it has more validation methods, including requiredif. http://foolproof.codeplex.com/

On the side not, there is a setting that you can turn off to disable implicit validation. look at this answer for details https://stackoverflow.com/a/4845768/1563373

Sign up to request clarification or add additional context in comments.

1 Comment

I'm going to try down the route of Nullable properties, cause that is what they can be.
0

I just disabled the form parts that I didn't want validated using JQuery and that worked.

<script type="javascript">
    $(function () {
        function EnableControls() {
             $("#Computer_Cpu").removeAttr("disabled");
             $("#Computer_Motherboard").removeAttr("disabled");
        }

        function DisableControls() {
             $("#Computer_Cpu").attr("disabled", "disabled");
             $("#Computer_Motherboard").attr("disabled", "disabled");
        }

        var disabled = true;
        $("#Enable_Controls_Checkbox").change(function () {
             if (disabled) {
                 EnableControls();
             } else {
                 DisableControls();
             }
        });

        DisableControls();
    })

</script>

As a run through, this script Disables all controls that I want, at load up. So the default is you can't edit them. There is a checkbox called Enable_Controls_Checkbox that when checked will enable said controls. attr(obj, obj) from my understanding adds/changes an attribute of the HTML Dom, and removeAttr(obj) as it says, removes an attribute.

There is probably a more elegant solution to this, but it works neither the less.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.