Reputation: 17701
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...
Upvotes: 0
Views: 84
Reputation: 1366
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);
}
}
Upvotes: 1
Reputation: 56688
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);
}
Upvotes: 1