2016-03-23 13 views
0

übergeben Ich versuche, Dateien und Parameter zu MVC-Controller-Aktion zu übergeben. Ich kann mein Modell, Dateien im Controller bekommen, aber kann nicht meine anderen Variablen in den Controllern wie string CommunId, string CommunContext bekommen, ich weiß, es könnte wegen dieser false Parameter sein, aber wenn ich es nicht auf false setze, wird meine Controller-Aktion nicht einmal aufgerufen. Hier ist mein Code:Wie mit FormData Dateien und Parameter zu mvc Controller-Aktion

function submitNewCommunication(s, e) { 
    // var data = new FormData($(this).serialize()); //tried this one also 

     $.ajax({ 
      url: '@Url.Action("CreateCommunication", "Communications")', 
      type: 'POST', 
      data: new FormData($('.createCommunicationForm').get(0)), 
      processData: false, 
      contentType: false, 
      success: function (result) { 
       alert('test'); 
      } 
     }); 

} 

My View

@using (Html.BeginForm("CreateCommunication", "Communications", new { CommunContext = ViewBag.CommContext, CommunId = ViewBag.CommId }, FormMethod.Post, new { @class = "createCommunicationForm"})) 
{ 
//some code 
} 

Meine MVC-Controller:

 public ActionResult CreateCommunication(string CommunId, string CommunContext, string[] PartyTypes, [ModelBinder(typeof(DevExpress.Web.Mvc.DevExpressEditorsBinder))]CommunicationModel item) 
    { 
     var files = Request.Files; 
    } 

So Dateien und Modell sind hier, aber nicht diese anderen Variablen, vielleicht ist es eine einfache Lösung wie kann ich sie erhalten? Danke für jede Hilfe und Ihre Zeit.

Antwort

1

Sie können die Werte übergeben Sie in der Form durch versteckte Felder erfordern:

@using (Html.BeginForm("CreateCommunication", "Communications", FormMethod.Post, new { @class = "createCommunicationForm"})) 
{ 
    <input type="hidden" name="CommunContext" value="@ViewBag.CommContext"> 
    <input type="hidden" name="CommunId" value="@ViewBag.CommId"> 
} 
Verwandte Themen