Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何用ASP.NET MVC调用C函数_C#_Asp.net_Asp.net Mvc_Asp.net Mvc 4_Razor - Fatal编程技术网

C# 如何用ASP.NET MVC调用C函数

C# 如何用ASP.NET MVC调用C函数,c#,asp.net,asp.net-mvc,asp.net-mvc-4,razor,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 4,Razor,我开始学习ASP.NET,并尝试检查文本框的格式是否正确。我可以检查格式是否正确,但我不知道如何连接前端的cshtml和后端的cs。下面是我在Index.cshtml中的代码 @{ ViewData["Title"] = "Home Page"; } <div> <h2>Robot Control Main Page</h2> <div class="row"> <div class="col-md-

我开始学习ASP.NET,并尝试检查文本框的格式是否正确。我可以检查格式是否正确,但我不知道如何连接前端的cshtml和后端的cs。下面是我在Index.cshtml中的代码

@{
    ViewData["Title"] = "Home Page";
}

<div>
    <h2>Robot Control Main Page</h2>
    <div class="row">
        <div class="col-md-2 col-md-offset-5">
            <span>
                <input type="text" size="20" name="ipAddr" placeholder="IP Address">
                <input type="button" name="btn_confirm" value="Confirm" onclick="CheckValidIP()">
            </span>
        </div>
    </div>
</div>

如何将按钮单击连接到C函数,并让相同的C函数访问span中的文本变量输入?

您想要的方式非常特殊,我建议您了解MVC中的验证工作方式。但是,如果必须在服务器上验证字段,则需要使用远程验证,如下所述:。

您要查找的内容在Asp.Net MVC中称为“远程验证”

首先确保在web.config中启用客户端验证

<appSettings>
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
第三个在模型中插入
[Remote]
属性。第一个参数是
操作
,第二个参数是
控制器

public class CreateUserModel : EditUserModel {
    [Remote("CheckValidIP", "Validation")]
    public override string ClientIP { get; set; }
}
最后但并非最不重要的一点是,您必须使用Html助手使用javascript等生成Html

@model RemoteValidation.Models.SampleModel

@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Remote Validation</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.ClientIp, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ClientIp, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ClientIp, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
@model RemoteValidation.Models.SampleModel
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
远程验证

@Html.ValidationSummary(true,“,new{@class=“text danger”}) @LabelFor(model=>model.ClientIp,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.ClientIp,new{htmlAttributes=new{@class=“form control”}}) @Html.ValidationMessageFor(model=>model.ClientIp,“,new{@class=“text danger”}) }
这是一个运行应用程序的示例,仅包含远程验证,Here=>

RemoteAttribute类创建一个字符串,该字符串表示用于调用基于服务器的验证的URL。然后,ASP.NET MVC框架为URL提交JSON编码的GET请求。 在此示例中,如果用户在客户端IP文本输入框中输入“127.0.0.1”,客户端将请求以下URL: /验证/检查ValidIP?ClientIP=127.0.0.1


在这里,您可以找到更多详细信息=>

您需要学习基础知识。检查这个微软官方链接,它不是那样工作的。MVC的概念与WebForms不同。您可以编写JavaScript函数来验证浏览器中的输入,或者将模型发布到操作并验证操作中的值。对于cshtml的输入,没有简单的方法来判断要执行什么C#函数。
public class CreateUserModel : EditUserModel {
    [Remote("CheckValidIP", "Validation")]
    public override string ClientIP { get; set; }
}
@model RemoteValidation.Models.SampleModel

@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Remote Validation</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.ClientIp, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ClientIp, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ClientIp, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>