0

My code is as followed and the error message are not displayed:

Index.cshtml

@model WebApp.Models.OrderItems
@using (Html.BeginForm("SaveToDB", "Home", FormMethod.Post, new { @class = "form-group", role = "form" }))
{

@Html.Partial("Information")

}

Partial : Information.cshtml

@model WebApp.Models.OrderItems

<div class="col-lg-4">
    <div class="form-group">
        <label for="input1" class="col-lg-4 control-label">@Html.LabelFor(model => model.CLInfo.ClientName)</label>
        @Html.EditorFor(model => model.CLInfo.ClientName, new { style = "width:250px" })
        @Html.ValidationMessageFor(model => model.CLInfo.ClientName)
    </div>
</div>

Model :

public class OrderItems
 {
      public InfoCLInfo{ get; set; }
 }

Model : the class for Infos

public class Info
 {
     [Display(Name = "Client Name")]
     [Required]
     public string ClientName{ get; set; }    
 }

The controller

    [HttpPost]
    [MultipleButton(Name = "action", Argument = "SaveToDB")]
    public ActionResult SaveToDB(OrderItems Client)
    {
      var errors = ModelState.Values.SelectMany(v => v.Errors);
        if (ModelState.IsValid)
        {
            if (_db == null)
                _db = new OrderDB();

            OrderItems ClientOrig = Session["Clientobj"] as OrderItems;
            ClientOrig.CLInfo = Client.CLInfo;

            Session["Clientobj"] = null;
        }

        return RedirectToAction("Index", "Home");
    }

    [Authorize]
    public ActionResult Index (OrderItems Client)
    {
        int ClientID = Convert.ToInt32(Session["Client"]);
        if ClientID == 0)
        {
            ClientID = 2;
            Session["Client"] = ClientID;
        }

        if (Session["Clientobj"] == null)
        {
            Client = new OrderItems();
            Client.CLOrderID = 123;
            Session["Clientobj"] = Client;
        }
        else
        {
            Client = Session["Clientobj"] as OrderItems
        }
        return View(Client);
    }

on post the ModelState.IsValid return false which true, but I don't have any message to tell the user where is the error to be fixed.

I tried to add : @Html.ValidationSummary(true) after the BeginForm , but it didn

Any idea please

Thanks

2
  • Can you post the related controller code as well? Commented Jun 22, 2014 at 20:02
  • @JustinHelgerson related controller code added Commented Jun 22, 2014 at 20:30

1 Answer 1

1

You cannot use RedirectToAction if you want to retain your model state. All errors and what not are kept in the ModelState object, and when you redirect to action it's performing a new get action, which starts fresh with a clean slate.

You need to return the view like you do in the original action.

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

3 Comments

Exactly why I asked for the related controller code!
Thanks,f I put : return View("Index"); in the Information.cshtml the Model is null for : <div class="form-group"> <label for="input1" class="col-sm-2 control-label">Country</label> @Html.DropDownListFor(model => model.CLGInfo.Country, new SelectList(Model.CLInfo.Country, "idCountry", "Country")) </div> the error message : Object reference not set to an instance of an object.
@ProtoCUS - I will repeat myself. You need to return the view LIKE YOU DO IN THE ORIGINAL ACTION, which means passing the model as well. So you return View("Index", Client)

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.