如何使用“required”htmlattribute向mvc 5 razor视图文本输入编辑器添加自定义错误消息

时间:2017-01-05 13:05:26

标签: c# asp.net asp.net-mvc razor

我对Asp.Net MVC很天真。

我有一个局部视图(ASP.Net MVC),其中我有一些必填字段,如果未提供任何必填字段,我想显示自定义错误消息。以下是我的部分视图的完整cshtml代码。

<div class="form-group">
        @Html.LabelFor(x => x.ServiceView.ListPriceView[i].LabourCost, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(x => x.ServiceView.ListPriceView[i].LabourCost, new { htmlAttributes = new { @class = "form-control", required = "required", **data_val_required = "LabourCost is requried"**} })
            @Html.ValidationMessageFor(x => x.ServiceView.ListPriceView[i].LabourCost,"", new { @class = "text-danger" })
        </div>
    </div>

我想显示自定义消息&#34;需要材料费用&#34;当我得到&#34;这个字段是必需的&#34;。所以我想在客户端覆盖这个difault错误消息。

我想实现这样的目标:

Kristians-MacBook-Pro:examples kristian$ mpicc -o hello hello_c.c
Kristians-MacBook-Pro:examples kristian$ mpiexec -n 4 ./hello
[Kristians-MacBook-Pro.local:02747] [[56076,0],0] bind() failed on error Address already in use (48)
[Kristians-MacBook-Pro.local:02747] [[56076,0],0] ORTE_ERROR_LOG: Error in file oob_usock_component.c at line 228
[Kristians-MacBook-Pro.local:02748] [[56076,1],0] usock_peer_send_blocking: send() to socket 19 failed: Socket is not connected (57)
[Kristians-MacBook-Pro.local:02748] [[56076,1],0] ORTE_ERROR_LOG: Unreachable in file oob_usock_connection.c at line 315
[Kristians-MacBook-Pro.local:02748] [[56076,1],0] orte_usock_peer_try_connect: usock_peer_send_connect_ack to proc [[56076,0],0] failed: Unreachable (-12)
[Kristians-MacBook-Pro.local:02749] [[56076,1],1] usock_peer_send_blocking: send() to socket 20 failed: Socket is not connected (57)
[Kristians-MacBook-Pro.local:02749] [[56076,1],1] ORTE_ERROR_LOG: Unreachable in file oob_usock_connection.c at line 315
[Kristians-MacBook-Pro.local:02749] [[56076,1],1] orte_usock_peer_try_connect: usock_peer_send_connect_ack to proc [[56076,0],0] failed: Unreachable (-12)
-------------------------------------------------------
Primary job  terminated normally, but 1 process returned
a non-zero exit code.. Per user-direction, the job has been aborted.
-------------------------------------------------------
--------------------------------------------------------------------------
mpiexec detected that one or more processes exited with non-zero status, thus causing
the job to be terminated. The first process to do so was:

  Process name: [[56076,1],0]
  Exit code:    1
--------------------------------------------------------------------------

任何建议/解决方案都会有很大的帮助

3 个答案:

答案 0 :(得分:25)

在模型类中,添加将[Required]属性更改为

[Required(ErrorMessage = "Material cost is required")]
public decimal MaterialCost {get;set;}

另一种方法是使用JQuery从JavaScript设置它或覆盖设置它的属性。默认情况下,ValidationMessageFor的输出为

data-val-required="The field is required.".

所以,您可以在标记中覆盖此值

答案 1 :(得分:9)

我找到了一种方法,通过使用htmlAttribute title 属性覆盖客户端上的默认必需消息,下面是代码:

<div class="form-group">
        @Html.LabelFor(x => x.ServiceView.ListPriceView[i].LabourCost, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(x => x.ServiceView.ListPriceView[i].LabourCost, new { htmlAttributes = new { @class = "form-control", required = "required", title = "LabourCost is requried"} })
            @Html.ValidationMessageFor(x => x.ServiceView.ListPriceView[i].LabourCost,"", new { @class = "text-danger" })
        </div>
    </div>

答案 2 :(得分:1)

在您的模型中

[Required(ErrorMessage = "Material cost is required")]
public doubleMaterialCost { get; set; }

如果您的网站中有多种文化,则可以选择从资源中加载它并传递资源字符串。

或在你的行动中

public ActionResult(YourModel model)
{
    if (model.doubleMaterialCost == 0)
            ModelState.AddModelError("doubleMaterialCost ", "Material cost is required");