Question

Hi I have got a drop downlist that I am binding that one in controller I have got one button in view with that I am doing some validations that's working fine,

when I submit the button for validation check i am not able to get the view with error message. Instead of this I am getting error like this " The view 'PostValues' or its master was not found or no view engine supports the searched locations". would any one help on why I am not able to get the view here the view is strongly Typed view and this is my code in controller.

public class CrossFieldsTxtboxesController : Controller
{
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Index()
        {
            var model = NewMethod();              
            return View(model);
        }    
        private static CrossFieldValidation NewMethod()
        {
            var model = new CrossFieldValidation
            {
                SelectedValue = "Amount",
                Items = new[]
                {
                     new SelectListItem { Value = "Amount", Text = "Amount" },
                     new SelectListItem { Value = "Pound", Text = "Pound" },
                     new SelectListItem { Value = "Percent", Text = "Percent" },
                }    
            };
            return model;
        }
        [HttpPost]
        public ActionResult PostValues(CrossFieldValidation model1)
        {               
            model1 =  NewMethod();
            if (!ModelState.IsValid)
            {    
                return View(model1);
            }
            else
            {
                return RedirectToAction("Index");                       
            }                                               
        }    
    }

and this is my view

@model MvcSampleApplication.Models.CrossFieldValidation   
@{
    ViewBag.Title = "Index";
}
<h2>Index</h2>
@using (Html.BeginForm("PostValues", "CrossFieldsTxtboxes"))
{   
    @Html.ValidationSummary(true)
    <div class ="editor-field">
      @Html.TextBoxFor(m => m.TxtCrossField)
       @Html.ValidationMessageFor(m=>m.TxtCrossField)
    </div>    
       @Html.DropDownListFor(m=> m.SelectedValue , Model.Items)
     <input id="PostValues" type="Submit" value="PostValues" />
}

would any one pls help on this...

Was it helpful?

Solution

This line

return View(model1);

looks for the view named exactly like the action in which it was called. Calling this line from PostValues action assumes there is a view PostValues.cshtml (which apparently does not exist). If you still want to use view Index - you should specify this explicitly:

if (!ModelState.IsValid)
{    
    return View("Index", model1);
}

OTHER TIPS

As Andrei said. Alternatively, you can give your PostValues method an additional tag:

[HttpPost, ActionName("Index")]
public ActionResult PostValues(CrossFieldValidation model1)
{
    if (!ModelState.IsValid)
    {    
        return View(model1);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top