繁体   English   中英

回发后无法看到视图

[英]not able to see the view after postback

嗨我有一个下拉列表,我绑定控制器中的一个我有一个按钮视图我正在做一些工作正常的验证,

当我提交验证检查按钮时,我无法获取带有错误消息的视图。 而不是这个我得到这样的错误“视图'PostValues'或其主人没有找到或没有视图引擎支持搜索的位置”。 任何人可以帮助我为什么我不能在这里获得视图视图是强类型视图,这是我在控制器中的代码。

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

这是我的观点

@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" />
}

任何人都会对此有所帮助......

这条线

return View(model1);

查找名称与调用它的操作完全相同的视图。 PostValues操作调用此行假设有一个视图PostValues.cshtml (显然不存在)。 如果您仍想使用视图Index - 您应该明确指定:

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

正如安德烈所说。 或者,您可以为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