问题描述
我有一个轻微的问题发送的FormCollection数据使用jQuery。员额法在我的MVC控制器的操作方法。虽然我通过jQuery发送数据,它是不是在我的动作控制器的参数的FormCollection,也让我更具体,它在那里,但并不完全怎么指望它会在那里...首先,该参数的FormCollection目前拥有2项......在PopID(21)通过加传入的序列化数据(FirstName=Fifo&LastName=Caputo&DateOfBirth=&DateOfBirth=7%2F29%2F2011+12%3A00%3A00+AM&City=&State=&Country=&Postal+$c$c=&deathIndicator=&email=&gender=&language=&NextOfKin=&Phone=)
我在做什么错在这里?控制器动作。
I am having a slight issue sending formCollection data to my controller action method in MVC using jQuery .Post method. While I am sending the data via jQuery, it is not in the formCollection parameter of my action controller, well let me be more specific, it is in there, but not quite how I expected it to be in there... First off, the formCollection parameter now has 2 entries... the PopID passed in (21) plus the serialized data passed in (FirstName=Fifo&LastName=Caputo&DateOfBirth=&DateOfBirth=7%2F29%2F2011+12%3A00%3A00+AM&City=&State=&Country=&Postal+Code=&deathIndicator=&email=&gender=&language=&NextOfKin=&Phone=)What am I doing wrong here? Controller Action.
[HttpPost]
public ActionResult SearchByDemographic(int PopID, FormCollection formCollection)
{
}
JavaScript的jQuery的方法在传递值...
JavaScript-Jquery method to pass in values...
$(function () {
$("#DemoGraphSubmit").click(function (e) {
e.preventDefault();
var form = $("#DemoGraphID");
var srlzdform = form.serialize();
var PopID = <% =PopID %>
var options = [];
var serializedForm = form.serialize();
$.post("/PatientACO/SearchByDemographic", {PopID:PopID,srlzdform:srlzdform}, function (data) {
options = $.map(data, function (item, i) {
return "<option value=" + item.Value + ">" + item.Text + "</option>";
});
$("#PatientListToAdd").html(options.join(""));
});
});
});
任何想法?我将继续寻找。
Any ideas? I am going to keep looking.
推荐答案
试试这个:
$.post("/PatientACO/SearchByDemographic", srlzdform, function (data) {
虽然我不知道你为什么要使用的FormCollection而不是一个视图模型的。
Although I am not sure why you want to use a FormCollection instead of a viewmodel.
也许你想看看
因此,这里是你的观点(显然比这更复杂)
So here is your view (obviously more complex than this)
@model TestModel
<form action="@Url.Action("SearchByDemographic","PatientACO")" method="post">
@Html.HiddenFor(model => model.PopID)
@Html.TextBoxFor(model => model.Text)
</form>
下面是你的行动:
[HttpPost]
public ActionResult SearchByDemographic(TestModel model)
{
}
这是你的模型
public class TestModel
{
public int PopID { get; set; }
public string Text { get; set; }
// more properties
}
如果这样,您存储PopID在模型中你没有这样做&LT;%:PopID%>在你的JavaScript
This way if you store your PopID in the model you don't have to do <%: PopID %> in your javascript.
这篇关于路过的FormCollection通过jQuery POST方法控制器和获取数据回来...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!