Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 如何将文本区域值传递给控制器_Jquery_Asp.net Mvc_Asp.net Mvc 4_C# 4.0 - Fatal编程技术网

Jquery 如何将文本区域值传递给控制器

Jquery 如何将文本区域值传递给控制器,jquery,asp.net-mvc,asp.net-mvc-4,c#-4.0,Jquery,Asp.net Mvc,Asp.net Mvc 4,C# 4.0,我有一个在循环中生成的多个文本区域的视图。我想使用jQuery在表单提交上传递值。我使用以下代码调用操作结果: var url = "/Template/DATA"; var form = $('<form action="' + url + '" method="post">' + '<input type="text" name="TemplateID" value="' + TemplateID + '" />' + '<input type=

我有一个在循环中生成的多个文本区域的视图。我想使用jQuery在表单提交上传递值。我使用以下代码调用操作结果:

var url = "/Template/DATA";
var form = $('<form action="' + url + '" method="post">' +
    '<input type="text" name="TemplateID" value="' + TemplateID + '" />' +
    '<input type="text" name="SectionID" value="' + SectionID + '" />' +
    '<input type="text" name="StartDate" value="' + StartDate + '" />' +
    '<input type="text" name="EndDate" value="' + EndDate + '" />' +
    // here i want to pass the values of the text areas to the action result
    '</form>');
$('body').append(form);
form.submit();
var url=“/Template/DATA”;
变量形式=$('')+
'' +
'' +
'' +
'' +
//在这里,我想将文本区域的值传递给操作结果
'');
$('body')。追加(表格);
表单提交();

如何使用html/jquery/ajax

<form id="form" action="" method="post">
    <input type="text" id="template" name="TemplateID" value="" />
    <input type="text" id="section" name="SectionID" value="" />
    <input type="text" id="start" name="StartDate" value="" />
    <input type="text" id="end "name="EndDate" value="" />
    <button type="submit">Submit</button>
</form>;
然后访问php中的数据,就像访问任何其他post数据一样

if($_POST){

    $template = $_POST['template'];
    $section = $_POST['section'];
    $start = $_POST['start'];
    $end = $_POST['end'];
    /** Do whatever with this data */ 

    $result = $template . $section . $start . $end
    echo json_encode(array('status' => 'ok', 'result' => $content));

} else {
    echo json_encode(array('status' => 'error'));
}

看起来最好使用AJAX,而不是动态构建表单的HTML,将其附加到DOM并立即提交?
if($_POST){

    $template = $_POST['template'];
    $section = $_POST['section'];
    $start = $_POST['start'];
    $end = $_POST['end'];
    /** Do whatever with this data */ 

    $result = $template . $section . $start . $end
    echo json_encode(array('status' => 'ok', 'result' => $content));

} else {
    echo json_encode(array('status' => 'error'));
}