2
When we put a button in a View, how do we fire a click of it? I have this button:
@model IEnumerable<CarregaDados.Models.Cliente>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Criar Usuário", "AdicionaUsuario")
</p>
<input type="button" id="btn" value="Teste" onclick=""/>
And I have this controller
public class IndexController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult Enviar()
{
string uri = "http://www.meu_site.com.br/autorizador/api/getliberaitens/value";
return null;
}
public static string HttpPost(string url, string postData)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
byte[] data = Encoding.ASCII.GetBytes(postData);
request.Method = "POST";
request.Accept = "text/plain";
request.ContentType = "text/plain; charset=utf-8";
request.ContentLength = data.Length;
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
return responseString;
}
}
How do I, by button, trigger any method or Action in the controller?
A web application has not been. Therefore, you do not "trigger" events/methods in controllers. You need to make your method meet some HTTP request and make the button send this request.
– Jéf Bueno
What do you want the button to do? Call a view or send data to the server?
– Jéf Bueno