简体   繁体   English

回发后无法看到视图

[英]not able to see the view after postback

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". 而不是这个我得到这样的错误“视图'PostValues'或其主人没有找到或没有视图引擎支持搜索的位置”。 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... 任何人都会对此有所帮助......

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). PostValues操作调用此行假设有一个视图PostValues.cshtml (显然不存在)。 If you still want to use view Index - you should specify this explicitly: 如果您仍想使用视图Index - 您应该明确指定:

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

As Andrei said. 正如安德烈所说。 Alternatively, you can give your PostValues method an additional tag: 或者,您可以为PostValues方法添加其他标记:

[HttpPost, ActionName("Index")]
public ActionResult PostValues(CrossFieldValidation model1)
{
    if (!ModelState.IsValid)
    {    
        return View(model1);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM