2017-04-09 16 views
-1

我開始學習ASP.NET,並試圖檢查文本框的格式是否正確。我可以檢查正確的格式,但我不知道如何連接前端中的cshtml和後端中的cs。下面是我的代碼從Index.cshtml如何使用ASP.NET MVC調用C#函數

@{ 
    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#HomeController類的功能。

//From my C# class 
public ActionResult CheckValidIP() 
{ 
    return View(); 
} 

如何將按鈕點擊連接到C#函數並讓相同的C#函數訪問跨度中的文本變量輸入?

+3

您需要從基礎知識中學習。檢查此MS官方鏈接https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/introduction/getting-started –

+0

它不起作用。 MVC與WebForms有不同的概念。您可以編寫JavaScript函數來驗證瀏覽器中的輸入,或者將模型發佈到操作並驗證操作中的值。有沒有簡單的方法來告訴什麼C#函數來執行從cshtml輸入。 –

回答

1

你正在尋找的東西在Asp.Net MVC中被稱爲「遠程驗證」。

第一個確保客戶端驗證在您的web.config中啓用。

<appSettings> 
    <add key="ClientValidationEnabled" value="true" /> 
    <add key="UnobtrusiveJavaScriptEnabled" value="true" /> 
</appSettings> 

在你的控制器創建驗證方法,將返回一個JsonResult。

public class ValidationController : Controller 
{ 
    public JsonResult CheckValidIP(string clientIP) 
    { 
     //your validation code here 
     if (!_repository.AllowIp(clientIP)) 
      return Json(true, JsonRequestBehavior.AllowGet); 
     else 
      return Json(false, JsonRequestBehavior.AllowGet); 
    } 
} 

模型插入[Remote]屬性。第一個參數是Action,第二個參數是Controller

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> 

這裏是正在運行的應用的樣品,只有在遠程驗證,這裏=>https://github.com/ricardodemauro/AspNetMVCRemoteValidation

的RemoteAttribute類創建表示URL用來調用基於服務器的驗證的字符串。 ASP.NET MVC框架然後提交URL的JSON編碼的GET請求。 在這個例子中,如果用戶在客戶端IP文本輸入框中輸入「127.0.0.1」,客戶端將請求如下網址: /Validation/CheckValidIP?ClientIP=127.0.0.1

在這裏你可以找到更多詳細信息=>https://msdn.microsoft.com/en-us/library/gg508808(vs.98).aspx

相關問題