2016-09-19 54 views
1

我們假設我有一個包含一些信息和表單的簡單頁面。如何用POST請求收集頁面的數據?

@using (Html.BeginForm("UpdateOrder", "OrderController", FormMethod.Post)) { 
     // some inputs here 
} 
<p id="user_info">Some text here</p> 

所有輸入的數據將以模型或FormCollection的形式發送到控制器。

但是,我還希望向控制器發送任何文本\圖像,通常來自位於表單之外的頁面的任何信息。這裏以id爲「user_info」的文本爲例。

我不知道它是否可以實現沒有jQuery,只能使用默認的控制器的功能。

+2

如果你不想使用jQuery/javascript,你應該將數據保存在帶有輸入字段的表單中,這樣當你提交表單時,它也會被提交給服務器。 – Shyju

+0

http://stackoverflow.com/questions/34764359/i-cant-get-the-value-of-my-file-input-asp-net-mvc-entity-framework – Tushar

+1

簡短的回答是沒有。表單只提交名稱/值對,其成功控件(input,textarea,select) –

回答

1

你可以簡單地做到這一點

1-如果你想上傳一些做cuments或圖像比你的形式應該是像 婁代碼:

@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" /> 
} 

比在POST方法countroller

[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); 
} 

這種方式,您可以上傳文件和數據都被包含跳這個幫助你

1

嘗試使用隱藏字段向控制器發送附加數據表單。

@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> 

OrderController控制器的操作方法

public ActionResult UpdateOrder(String user_info) 
{ 
//Hidden filed **name** will be the name of the String 
//From collection can be used in similar way 
} 

編輯:您可以通過的jQuery/JavaScript的更新隱藏字段的值,並提交後,你可以在控制器和文字得到更新值/圖像是略有不同