I am just simply trying to pass my model in my method as a parameter so I have all the data in my model. I can put the model in a Viewbag to pass it in that works fine. But why cant I pass it in as a parameter instead? The button I am using is on a modal
which is inside of the main view.
<div id="zipModal" class="modal" role="dialog" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
@using (Html.BeginForm("UnZipDownload", "Service", FormMethod.Post, new { model = Model }))
{
<div class="modal-header text-center">
</div>
<div class="modal-body text-center">
</div>
<div class="modal-footer">
@*Html.ActionLink("OK", "UnZipDownload", "Service", new { @model = Model, @class = "btn btn-success pull-right" })*@
<input type="submit" name="submit" class="btn btn-success pull-right" value="OK"/>
</div>
}
</div>
</div>
</div>
[HttpPost]
public ActionResult UnZipDownload(ServiceModel model)
{
if(TempData["ZipName"] != null)
{
bool isValid = false;
string zipName = TempData["ZipName"] as string;
string downloadPath = new KnownFolder(KnownFolderType.Downloads).Path;
string filePath = new KnownFolder(KnownFolderType.Downloads).Path;
downloadPath = Path.GetFullPath(downloadPath);
//Make sure last char on path doesn't allow malicious code outside of it.
if (!downloadPath.EndsWith(Path.DirectorySeparatorChar.ToString(), StringComparison.Ordinal))
downloadPath += Path.DirectorySeparatorChar;
string zipFolder = zipName.Substring(0, zipName.Length - 4);
Directory.CreateDirectory(Path.Combine(downloadPath, zipFolder));
TempData["FolderName"] = zipFolder;
using (ZipArchive archive = ZipFile.OpenRead(filePath + "\\" + zipName)) // downloadPath + zipName
{
filePath += "\\" + zipFolder; // /?/ downloadPath + zipFolder ?? ??
foreach(ZipArchiveEntry entry in archive.Entries)
{
if(entry.FullName.EndsWith(".pdf", StringComparison.OrdinalIgnoreCase))
{
string destinationPath = Path.GetFullPath(Path.Combine(filePath, entry.FullName));
entry.ExtractToFile(destinationPath);
}
}
// Unzipped all files into new folder completed
isValid = true;
}
var mod = ViewBag.ServModel;
var servModel = TempData["ServModel"];
if (isValid)
{
ViewBag.AlertSuccess = "The .zip file has been successfully unzipped";
OpenPDFs();
}
}
return RedirectToAction("VirtualService", model);
}
the route values specified in a form, are added to the url in the action attribute as url parameters. url parameters are name/value pairs. to render the value, the ToString() method is called. you could do instead:
when razor render @Model, it just calls ToString (and url encodes the output). Generally if called on an object, ToString() returns the class name. To pass the model properties in the url, you need to specify each property you want to pass:
Member
31 Points
231 Posts
Not understanding why my Model isn’t passing in the method as a parameter.
Nov 13, 2019 04:23 PM|ExceedingLife|LINK
Hello all,
I am just simply trying to pass my model in my method as a parameter so I have all the data in my model. I can put the model in a Viewbag to pass it in that works fine. But why cant I pass it in as a parameter instead? The button I am using is on a modal which is inside of the main view.
All-Star
58194 Points
15661 Posts
Re: Not understanding why my Model isn’t passing in the method as a parameter.
Nov 13, 2019 06:18 PM|bruce (sqlwork.com)|LINK
the route values specified in a form, are added to the url in the action attribute as url parameters. url parameters are name/value pairs. to render the value, the ToString() method is called. you could do instead:
<form method="post" action="@Url.Action("UnZipDownload","Service")?model=@Model" >
when razor render @Model, it just calls ToString (and url encodes the output). Generally if called on an object, ToString() returns the class name. To pass the model properties in the url, you need to specify each property you want to pass:
@using (Html.BeginForm("UnZipDownload", "Service", FormMethod.Post, new { Prop1 = Model.Prop1, Prop2 = Model.Prop2 }))
note: there is a max length to a url (about 2k), so you need to keep it short.
Participant
980 Points
198 Posts
Re: Not understanding why my Model isn’t passing in the method as a parameter.
Nov 15, 2019 06:28 AM|Wenushka|LINK
Hi,
You could submit them as hidden inputs.
May be you could refactor the code to pass few properties. Or if create the model object in the POST action method
public ActionResult UnZipDownload
in the controller before passing it to the next call.
Thanks,
Wenushka