加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc-4 – MVC 4 – 从视图中的Controller -Show返回错误

发布时间:2020-12-16 00:39:54 所属栏目:asp.Net 来源:网络整理
导读:我在VS2010中使用Razor做了一个C#项目(MVC 4)。 我需要从控制器返回错误信息查看并显示给用户。 我试过的是: 控制器: [HttpPost] public ActionResult form_edit(FormModels model) { model.error_msg = model.update_content(model); ModelState.AddModel
我在VS2010中使用Razor做了一个C#项目(MVC 4)。
我需要从控制器返回错误信息查看并显示给用户。
我试过的是:

控制器:

[HttpPost]
 public ActionResult form_edit(FormModels model)
 {          
    model.error_msg = model.update_content(model);           
    ModelState.AddModelError("error","adfdghdghgdhgdhdgda");
    ViewBag.error = TempData["error"];
    return RedirectToAction("Form_edit","Form");

 }

视图:

@model mvc_cs.Models.FormModels
@using ctrlr = mvc_cs.Controllers.FormController


@using (Html.BeginForm("form_edit","Form",FormMethod.Post))
{

        <table>
        <tr>
        <td>       
        @Html.ValidationSummary("error")       
        @Html.ValidationMessage("error")      
        </td>
        </tr>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.content_name)
            @Html.DropDownListFor(x => x.selectedvalue,new SelectList(Model.Countries,Model.dd_value,Model.dd_text),"-- Select Product--")

        </th>
    </tr>
</table>

<table>
    <tr>
        <td>
            <input  type="submit" value="Submit" />
        </td>
    </tr>
</table>
}

请帮我实现这个。

解决方法

返回视图(模型)会返回错误,因为您没有使用Post方法中的值填充模型,并且下拉列表的模型数据为空。请提供Get方法进一步说明如何管理显示错误。为了显示错误,您应该使用:
[HttpPost]
public ActionResult form_edit(FormModels model)
{          
   if(ModelState.IsValid())
   {
      --- operations 
      return Redirect("OtherAction","SomeController");
   }

   // here you can use a little trick
   //fill the model property that holds the information for the dropdown with the data 

   // you haven't provided the get method but it should look something like this
   model.Countries = ... some data goes here;
   model.dd_value = ... some other data;
   model.dd_text = ... other data;

   ModelState.AddModelError("","adfdghdghgdhgdhdgda");
   return View(model);
}

然后在视图中只使用:

@model mvc_cs.Models.FormModels
@using ctrlr = mvc_cs.Controllers.FormController


@using (Html.BeginForm("form_edit",FormMethod.Post))
{

    <table>
    <tr>
    <td>       
    @Html.ValidationSummary(true)             
    </td>
    </tr>
<tr>
    <th>
        @Html.DisplayNameFor(model => model.content_name)
        @Html.DropDownListFor(x => x.selectedvalue,"-- Select Product--")

    </th>
</tr>
</table>

<table>
<tr>
    <td>
        <input  type="submit" value="Submit" />
    </td>
</tr>
</table>
}

这应该可以正常工作。

如果您只是使用RedirectToAction,它将重定向到get方法 – >您将没有错误,但视图将被重新加载,并且不会显示任何错误。

其他方法是,您可以传递错误不是由ModelState.AddError,但与ViewData [“错误”]像这样:

[HttpPost]
public ActionResult form_edit(FormModels model)
{          
   TempData["error"] = "someErrorMessage";
   return RedirectToAction("form_Post","Form");
}

[HttpGet]
public ActionResult form_edit()
{
    do stuff here ----
    ViewData["error"] = TempData["error"];
    return View();
}

@model mvc_cs.Models.FormModels
@using ctrlr = mvc_cs.Controllers.FormController


@using (Html.BeginForm("form_edit",FormMethod.Post))
{

    <table>
    <tr>
    <td>       
    <div>@ViewData["error"]</div>             
    </td>
    </tr>
<tr>
    <th>
        @Html.DisplayNameFor(model => model.content_name)
        @Html.DropDownListFor(x => x.selectedvalue,"-- Select Product--")

    </th>
</tr>
</table>

<table>
<tr>
    <td>
        <input  type="submit" value="Submit" />
    </td>
</tr>
</table>
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读