Your form doesn't contain any input tag other than the file so in your controller action you cannot expect to get anything else than the uploaded file (that's all that's being sent to the server). One way to achieve this would be to include a hidden tag containing the id of the model which will allow you to retrieve it from your datastore inside the controller action you are posting to (use this if the user is not supposed to modify the model but simply attach a file):
@using (Html.BeginForm("Uploadfile", "Containers", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.HiddenFor(x => x.Id)
<input type="file" name="file" id="file" />
<input type="submit" value="submit" />
}
and then in your controller action:
[HttpPost]
public ActionResult Uploadfile(int id, HttpPostedFileBase file)
{
Containers containers = Repository.GetContainers(id);
...
}
On the other hand if you wanted to allow the user to modify this model then you will need to include the proper input fields for each field of your model that you want to be sent to the server:
@using (Html.BeginForm("Uploadfile", "Containers", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.TextBoxFor(x => x.Prop1)
@Html.TextBoxFor(x => x.Prop2)
@Html.TextBoxFor(x => x.Prop3)
<input type="file" name="file" id="file" />
<input type="submit" value="submit" />
}
and then you will have the default model binder reconstruct this model from the request:
[HttpPost]
public ActionResult Uploadfile(Container containers, HttpPostedFileBase file)
{
...
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…