C# 如何收集第'页;有POST请求的数据吗?
让我们假设我有一个包含一些信息和表单的简单页面C# 如何收集第'页;有POST请求的数据吗?,c#,jquery,asp.net,asp.net-mvc,C#,Jquery,Asp.net,Asp.net Mvc,让我们假设我有一个包含一些信息和表单的简单页面 @using (Html.BeginForm("UpdateOrder", "OrderController", FormMethod.Post)) { // some inputs here } <p id="user_info">Some text here</p> @使用(Html.BeginForm(“UpdateOrder”、“OrderController”、FormMethod.Post)){ /
@using (Html.BeginForm("UpdateOrder", "OrderController", FormMethod.Post)) {
// some inputs here
}
<p id="user_info">Some text here</p>
@使用(Html.BeginForm(“UpdateOrder”、“OrderController”、FormMethod.Post)){
//这里有一些输入
}
此处有一些文本
所有输入的数据将像模型一样或通过FormCollection发送到控制器
但是,我还希望向控制器发送任何文本\图像,通常是来自表单外部页面的任何信息。这里以id为“用户信息”的文本为例
我想知道它是否可以在不使用jQuery的情况下实现,只使用默认控制器的功能。尝试使用隐藏字段向控制器发送额外的数据表单
@using (Html.BeginForm("UpdateOrder", "Order", FormMethod.Post)) {
// some inputs here
<input type="hidden" name="user_info" id="user_info" value="Norway">
}
//<p id="user_info">Some text here</p>
编辑:您可以通过jQuery/javascript更新隐藏字段值,提交后,您可以在控制器和文本/图像中获得更新的值。这略有不同。您可以简单地完成
1-如果你想上传一些文档或图像,那么你的表单应该是这样的
以下代码:
@using (Html.BeginForm("ApplyOnline", "Applieds", null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<input type="hidden" name="JobId" id="JobId" value="@ViewBag.JobId" />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
<label class="control-label col-md-3">First Name (اسم)</label>
<div class="col-md-8">
@Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control",@required="required" } })
@Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
</div>
</div>
<input type='file' name='pmd' id='pmd' />
<input type="submit" value="Apply" class="btn btn-primary" />
}
通过这种方式,您可以上载文件和数据。此帮助包括所有内容。如果您不想使用jQuery/javascript,则应使用一个输入字段将数据保存在表单中,以便在提交表单时也将其提交到服务器。简短回答是否。表单仅提交其成功控件的名称/值对(输入,文本区域,选择)
@using (Html.BeginForm("ApplyOnline", "Applieds", null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<input type="hidden" name="JobId" id="JobId" value="@ViewBag.JobId" />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
<label class="control-label col-md-3">First Name (اسم)</label>
<div class="col-md-8">
@Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control",@required="required" } })
@Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
</div>
</div>
<input type='file' name='pmd' id='pmd' />
<input type="submit" value="Apply" class="btn btn-primary" />
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult ApplyOnline([Bind(Include = "Id,JobId,FirstName")] Applied applied, HttpPostedFileBase pmd, int JobId)
{
if (ModelState.IsValid)
{
//---save the data---------//
db.MyAppliedContext.Add(applied);
db.SaveChanges();
//---Get inserted Id----//
int insertedId = applied.Id;
//--------Upload PMD-------------------//
if (pmd != null && pmd.ContentLength > 0)
{
try
{
var PMDFileName = "PMD-" + applied.JobId + "-" + TrimedUser + "-" + insertedId + "-" + pmd.FileName;
//var P11FileName = DateTime.Now.ToString();
string path = Path.Combine(Server.MapPath("~/App_Data/system"),
Path.GetFileName(PMDFileName));
pmd.SaveAs(path);
UploadFiles MyPMDUploads = new UploadFiles();
MyPMDUploads.JobId = applied.JobId;
MyPMDUploads.ApplyId = insertedId;
MyPMDUploads.FileName = Path.GetFileName(PMDFileName);
MyPMDUploads.FilePath = path;
db.MyUploadFileContext.Add(MyPMDUploads);
db.SaveChanges();
ViewBag.Message = "PMD uploaded successfully";
}
catch (Exception ex)
{
ViewBag.Message = "ERROR PMD:" + ex.Message.ToString();
}
}
else
{
ViewBag.Message = "You have not specified a PMD file.";
}
}
return view(Model);
}