0

I'm building a validation form in my application. In that form there are two buttons. One to accept and one to reject. When the user press reject the rejection reason field must be provided. I check this serverside. I first check what button is pressed and then if the field is empty I add a moddel error to the modelstate. But, because all fields in the form are readonly, those are not posted back to the server and therefor when I return the view back to usern there is no data. I'm probably missing something obvious, but cant find what to do. (I know I can make all fields in my form hidden, but due to the large amount of fields this would be really ugly)

This is my code.

    [HttpPost]
    public virtual ActionResult Validate(string action, Record dto) {

        if(action == Global.Accept) {
            ciService.Store(dto);
            return RedirectToAction("Index", "Ci");
        } else {
            if(string.IsNullOrEmpty(dto.RejectionReason)) {
                ModelState.AddModelError("RejectionReason", "REQUIRED!!!!");
                return View("Validate", dto);


            }
            ciService.Reject(dto);
            return RedirectToAction("Index", "Ci");
        }
    }
4
  • Can i suggest making two action methods, one for accept and one for reject. Action named Validate does not seem the right place to be doing ciService.Store(dto); Commented Mar 7, 2012 at 17:41
  • What do you mean by readonly fields? There are readonly and disabled attributes that you could use on your input tags? Is it what you are talking about? Note that while both attributes prevent the user from modifying the values in those fields, the difference is that readonly input fields still POST their values to the server which is not the case with disabled fields. Commented Mar 7, 2012 at 17:42
  • @DarinDimitrov I think he means fields on the model class are readonly. Default MVC templates would then just display them as text and not as HTML form fields Commented Mar 7, 2012 at 17:43
  • @RobertLevy, so conditional validation? Commented Mar 7, 2012 at 17:44

5 Answers 5

2

You need to recreate the model from the database and then change it to match whatever changes are posted in dto. Then use that combined model in the view.

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

1 Comment

It is only one field the user can fill in in this form, so this is what I did. Thanks
1

Instead of passing the DTO back from the browser, I would use a hidden HTML field or a querystring parameter containing the ID that identifies the DTO. Then your POST action method would look something like:

[HttpPost]
public virtual ActionResult Validate(string action, int id)
{
    // reload the DTO using the id
    // now you have all the data, so just process as you did in your question

    if (action == Global.Accept) { ... }
    ...
}

Your GET method might look something like the following then...

[HttpGet]
public virtual ActionResult Validate(int id)
{
    // load the DTO and return it to the view

    return View();
}

In this way you have all the data you need within your POST action method to do whatever you need.

Comments

0

You need to have hidden fields corresponding to each property displayed in UI.

e.g.,

@Html.LabelFor(m=>m.MyProperty) - For Display

@Html.Hiddenfor(m=>m.MyProperty) - ToPostback the value to server

Comments

0

If I understand right, the problem is because you don't use input. To solve your problem insert some input hidden in your form with the value you need to be passed to the controller

 @Html.HiddenFor(model => model.Myfield1) 
 @Html.HiddenFor(model => model.Myfield2) 

that should fix the values not passed back to your actions

Comments

0

If you don't need these fields on the server side, simply create a new ViewModel RecordValidateViewModel and this contains only the fields in it that need to be validated. The model binder will populate then and you will have validation only on the fields in that model rather than all the other fields you don't seem to want there.

If you need them to validate, then post them back to the server. Its not 'ugly' if hidden.

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.