Patrick
Patrick

Reputation: 2790

asp.net mvc Serverside validation no return data

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");
        }
    }

Upvotes: 0

Views: 242

Answers (5)

Jason Iwinski
Jason Iwinski

Reputation: 383

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.

Upvotes: 1

Adam Tuliper
Adam Tuliper

Reputation: 30152

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.

Upvotes: 0

Iridio
Iridio

Reputation: 9271

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

Upvotes: 0

Manas
Manas

Reputation: 2542

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

Upvotes: 0

Robert Levy
Robert Levy

Reputation: 29073

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.

Upvotes: 2

Related Questions