Ich habe eine Textbox und einige Optionsfelder. Zum Beispiel sieht mein HTML-Code-Eingabe-HTML so aus:
<input type="text" name="IP" id="IP" />
Sobald der Benutzer auf eine Schaltfläche auf einer Webseite klickt, möchte ich Daten an meinen Controller weitergeben:
<input type="button" name="Add" value="@Resource.ButtonTitleAdd" onclick="location.href='@Url.Action("Add", "Configure", new { ipValue [email protected][ValueOfTextBox], TypeId = 1 })'"/>
Vielleicht ist es trivial, aber mein Problem ist, dass ich nicht weiß, wie ich einen Textfeldwert erhalte und ihn an den Controller weitergeben kann. Wie kann ich den Textfeldwert lesen und über [email protected][ValueOfTextBox]
an die Steuerung übergeben?
Ein einfaches ASP.NET MVC-Abonnementformular mit E-Mail-Textfeld würde folgendermaßen implementiert:
Die Daten aus dem Formular werden diesem Modell zugeordnet
public class SubscribeModel
{
[Required]
public string Email { get; set; }
}
Der Ansichtsname sollte mit dem Namen der Controller-Methode übereinstimmen.
@model App.Models.SubscribeModel
@using (Html.BeginForm("Subscribe", "Home", FormMethod.Post))
{
@Html.TextBoxFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email)
<button type="submit">Subscribe</button>
}
Der Controller ist für die Anforderungsverarbeitung verantwortlich und gibt die richtige Antwortansicht zurück.
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Subscribe(SubscribeModel model)
{
if (ModelState.IsValid)
{
//TODO: SubscribeUser(model.Email);
}
return View("Index", model);
}
}
Hier ist meine Projektstruktur. Bitte beachten Sie, dass der Ordner "Startseite" mit dem Namen des HomeControllers übereinstimmt.
Sie können jQuery verwenden:
<input type="text" name="IP" id="IP" value=""/>
@Html.ActionLink(@Resource.ButtonTitleAdd, "Add", "Configure", new { ipValue ="xxx", TypeId = "1" }, new {@class = "link"})
<script>
$(function () {
$('.link').click(function () {
var ipvalue = $("#IP").val();
this.href = this.href.replace("xxx", ipvalue);
});
});
</script>
Versuche dies.
Aussicht:
@using (Html.BeginForm("Login", "Accounts", FormMethod.Post))
{
<input type="text" name="IP" id="IP" />
<input type="text" name="Name" id="Name" />
<input type="submit" value="Login" />
}
Controller:
[HttpPost]
public ActionResult Login(string IP, string Name)
{
string s1=IP;//
string s2=Name;//
}
Wenn Sie Modellklasse verwenden können
[HttpPost]
public ActionResult Login(ModelClassName obj)
{
string s1=obj.IP;//
string s2=obj.Name;//
}
Eine andere Möglichkeit mit der Ajax-Methode:
Aussicht:
@Html.TextBox("txtValue", null, new { placeholder = "Input value" })
<input type="button" value="Start" id="btnStart" />
<script>
$(function () {
$('#btnStart').unbind('click');
$('#btnStart').on('click', function () {
$.ajax({
url: "/yourControllerName/yourMethod",
type: 'POST',
contentType: "application/json; charset=utf-8",
dataType: 'json',
data: JSON.stringify({
txtValue: $("#txtValue").val()
}),
async: false
});
});
});
</script>
Controller:
[HttpPost]
public EmptyResult YourMethod(string txtValue)
{
// do what you want with txtValue
...
}
sie können es so einfach machen:
Erstens: Zum Beispiel in Models haben Sie User.cs mit dieser Implementierung
public class User
{
public string username { get; set; }
public string age { get; set; }
}
Wir übergeben das leere Modell an den Benutzer - Dieses Modell würde mit den Daten des Benutzers gefüllt, wenn er das Formular so sendet
public ActionResult Add()
{
var model = new User();
return View(model);
}
Wenn Sie die Ansicht nach leerem Benutzer als Modell zurückgeben, wird die Struktur des von Ihnen implementierten Formulars zugeordnet. Wir haben dies auf HTML-Seite:
@model MyApp.Models.Student
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Student</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.username, htmlAttributes: new {
@class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.username, new {
htmlAttributes = new { @class = "form-
control" } })
@Html.ValidationMessageFor(model => model.userame, "",
new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.age, htmlAttributes: new { @class
= "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.age, new { htmlAttributes =
new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.age, "", 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>
}
Beim Button Submit werden Sie es also so verwenden
[HttpPost]
public ActionResult Add(User user)
{
// now user.username has the value that user entered on form
}