我是mvc的新手,我想知道如何将文本框文本分配给mvc中的另一个变量。
在windows中我们可以使用textbox1.text = var body;
如何使用以下代码
进行类似的操作我想在textbox中编写文本,以便调用PAF控制器索引操作的字符串邮政编码参数?
@Html.TextBoxFor(model => model.PostCode)
**Here we will get : postcode: ------------(Assume Postcode:52345)**
<input type="button" name="postcode" value="check your address" onclick="location.href='@Url.Action("Index", "PAF", new {Postcode = ...... )})'" />
**Here we will get : check your address button**
这是我的PAF控制器索引操作
public class PAFController : Controller
{
public ActionResult Index(string Postcode)
{
return View();
}
}
答案 0 :(得分:1)
您需要执行诸如
之类的操作onclick="location.href='@Url.Action("Index", "PAF")?Postcode=' + $('#postcode').val()"
假设您为输入文本框提供了邮政编码的ID:
@Html.TextBoxFor(model => model.PostCode, new { id = "postcode" });
答案 1 :(得分:0)
在这里,我得到了上述问题的答案:我使用了Jquery Ajax
** added data-attribute to the button**
<input type="button" data-paf-url="/paf/index" id="postcodebutton"
value="check your address" />
**added Id for the textbox**
@Html.TextBoxFor(model => model.PostCode,
new { @class = "form-control",id="postcode", placeholder="Postcode" })
<script>
$(document).ready(function () {
$("#postcodebutton").click(function (e) {
e.preventDefault();
var postc= $("#postcode").val();
$.ajax({
url: $(this).attr('data-paf-url'),
type: 'GET',
data:{Postcode:postc},
success: function (data, textStatus, jqXHR) {
// my code
},
error: function (jqXHR, textStatus, errorThrown) {
alert('An error occured status' + textStatus + 'error thrown:' + errorThrown);
}
});
});
});
</script>