Dropzone FileUpload - c#

Dear friends I am currently creating an admin panel for user where they can easily can publish their articles. I also want to add to my form a little fileuploader but sadly I got some problems with DROPZONEJS.JS file in POST method. The main problem is I cannot give URL to project's local file in order to download to file there where website will access those file in order to publish them with current article's id. Please let me know if there is something not understandable.
#{
ViewBag.Title = "Uppy";
}
#{
Layout = "~/Views/Shared/_AdminLayout.cshtml";
}
<div class="content d-flex flex-column flex-column-fluid" id="kt_content">
<!--begin::Entry-->
<div class="d-flex flex-column-fluid">
<!--begin::Container-->
<div class="container">
<!--begin::Card-->
<div class="card card-custom gutter-b">
<div class="card-header">
<div class="card-title">
<h3 class="card-label">File Upload</h3>
</div>
</div>
<!--begin::Form-->
<form>
<div class="card-body">
<div class="form-group row">
<label class="col-form-label col-lg-3 col-sm-12 text-lg-right">Multiple File Upload</label>
<div class="col-lg-4 col-md-9 col-sm-12">
<div class="dropzone dropzone-default dropzone-primary" id="kt_dropzone_2">
<div class="dropzone-msg dz-message needsclick">
<h3 class="dropzone-msg-title">Drop files here or click to upload.</h3>
<span class="dropzone-msg-desc">Upload up to 10 files</span>
</div>
</div>
</div>
</div>
<div class="form-group row">
<label class="col-form-label col-lg-3 col-sm-12 text-lg-right">File Type Validation</label>
<div class="col-lg-4 col-md-9 col-sm-12">
<div class="dropzone dropzone-default dropzone-success" id="kt_dropzone_3">
<div class="dropzone-msg dz-message needsclick">
<h3 class="dropzone-msg-title">Drop files here or click to upload.</h3>
<span class="dropzone-msg-desc">Only image, pdf and psd files are allowed for upload</span>
</div>
</div>
</div>
</div>
</div>
#using (Html.BeginForm("Uppy",
"FileUpload",
FormMethod.Post,
new { enctype = "multipart/form-data" })) //multipart/form-data gives functionlity to inputes (search at web);
{
<div class="card-footer">
<div class="row">
<div class="col-lg-3"></div>
<div class="col-lg-4">
<input type="submit" value="Submit" class="btn btn-light-primary mr-2" />
<button type="reset" class="btn btn-primary">Cancel</button>
</div>
</div>
</div>
}
</form>
<!--end::Form-->
</div>
<!--end::Card-->
<!--end::Row-->
</div>
<!--end::Container-->
</div>
<!--end::Entry-->
</div>
<!--end::Content-->
By the way my own upload code is working correctly and also sends choosen file to url location where I wrote in controller.
<label for="file">Upload File:</label>
<input type="file" name="file" id="file" class="btn-hover-bg-success" />
<br>
<br>
<input type="submit" value="Upload Image" />
<br>
<br>
#ViewBag.Message
This is my FileUploadController:
[HttpPost]
public ActionResult Uppy(HttpPostedFileBase file)
{
ADAPTIVE_TESTEntities ent = new ADAPTIVE_TESTEntities();
Adaptive.News.Models.NEWS news = new Adaptive.News.Models.NEWS();
if (file != null && file.ContentLength > 0)
try
{
var path = Path.Combine(Server.MapPath("~/Content/images"),
Path.GetFileName(file.FileName));
file.SaveAs(path);
news.PICTUREPATH = path;
ent.NEWS.Add(news);
ent.SaveChanges();
ViewBag.Message = "File uploaded successfully";
}
catch (Exception ex)
{
ViewBag.Message = "ERROR: " + ex.Message.ToString();
}
else
{
ViewBag.Message = "You have not specified a file.";
}
return View();
}

it is not obvious to me, what and where really your problem is. What do you mean with having problem. Is your problem with Dropzone.JS or with C#? Anyway, I examined your code slightly and have some ideas what your problem could be.
FIRST OF ALL: You have 2 DIV containers, which you use as Dropzone elements assigning them the css class "dropzone". Furthermore you generate a FORM element with ASP.Net HTML-Helper.
By default you have 2 options using Dropzone.
Declarative instantiation via assigning css class "dropzone" to any HTML element.
Dropzone discovers all DOM elements with class="dropzone" automatically and instantiates them.
Programmatically instantiating: You instantiate Dropzone by passing the id of the container element and THE OPTIONS CONTAINING POST URL to the Dropzone constructor.
DECLARATIVE DROPZONE
You must pay attention to this detail: If you use FORM element as Dropzone container element, Dropzone uses "action" attribute of the FORM element as post URL. But if you use
DIV element as container, then you get most possibly a JavaScript error. Because DIV elements do usually NOT have "action" attribute. If you use DIV as Dropzone container (and in your code you use it 2 times), you will get following JavaScript error:
dropzone.js:1027 Uncaught Error: No URL provided.
at new Dropzone (dropzone.js:1027)
at dropzone.js:2907
at Function.Dropzone.discover (dropzone.js:2919)
at Dropzone._autoDiscoverFunction (dropzone.js:3491)
at HTMLDocument.init (dropzone.js:3456)
In this case you have two options solve the problem:
Use FORM element instead of DIV as Dropzone container.
Add action="/your/post/url" to your DIV element, which you use as Dropzone container.
Where I would prefer the first option. Because it is not common that DIV elements have action attribute.

Related

Problem with saving value with the input type number to the chosen field to the database by C# in ASP .NET MVC

I have a problem with implement saving value with input type number to the database. I have created website with ASP .NET MVC with UnitOfWork and Repositories. I have a Commodity model, where I have field Amount, and I have HomeViewModel in which I have a field Amount also. I would like to overwrite the amount field from the commodity model. Should I use onchange or onclick in input or a tags in html code, or all with my basic ideas are nonsens ?
Below is the code snippet with view created in RazorPages with extension .cshtml.
<div class="col-12">
<div class="row my-3">
#foreach(var Commodity in Model.CommodityList.Where(c=>c.CategoryId==category.Id))
{
<div class="col-lg-4 col-xl-3 col-md-6 pb-4">
<div class="card" style="border:2px solid #43ac6a;border-radius:5px;
backgroundcolor: #fffeee">
<div class="pl-3 pt-1 text-center">
<h3 class="card-title text-primary"><b>#Commodity.Name</b></h3>
</div>
<div class="d-flex justify-content-between">
<div class="pl-1 text-muted">#localizer.Get("Price")</div>
<div class="pr-1 text-danger h5">$#Commodity.Price</div>
</div>
<img src="#Commodity.ImageUrl" class="card-img-top p-2 rounded" />
<div class="col-9">
<input onchange="save()" asp-for="#Commodity.Amount" type="number"
id="quantity" name="quantity" min="0" max="100" />
</div>
<a onclick="save()" asp-action="Details" class="btn btn-success"
style="border-
radius:2px;" asp-route-id="#Commodity.Id"
onclick="save()">#localizer.Get("Details")</a>
</div>
</div>
}
</div>
</div>
My problem is that I can't write the value from after pressing the button below as the tag.
Below is the method written by me in controller regarding to the view
public IActionResult Index()
{
HomeVM = new HomeViewModel()
{
CategoryList = _unitOfWork.Category.GetAll(),
CommodityList = _unitOfWork.Commodity.GetAll(includeProperties: "Category"),
ServiceList = _unitOfWork.Service.GetAll(includeProperties: "Category,Payment"),
EmployeeList = _unitOfWork.Employee.GetAll(includeProperties: "Service"),
Amount = _unitOfWork.Commodity.GetFirstOrDefault(includeProperties:
"Category").Amount
};
foreach (var commodity in (_unitOfWork.Commodity.GetAll(includeProperties:
"Category")))
{
var CommodityFromDb = commodity;
CommodityFromDb.Amount = HomeVM.Amount;
_unitOfWork.Save();
}
_unitOfWork.Save();
return View(HomeVM);
}
I have a guess that you probably need to do it somehow with jquery, in a file with the extension js, but I have no idea how. I would be grateful for any help.

How to get html input button to properly display resource.resx key / value entry?

This is a simplified demonstration of the issue. will not display a string separated by white space.
#{
var myString = "worda wordb wordc";
}
<div class="row">
<input value=#myString>
</div>
Using a C# resource file for multi language support, I'm having problems with web page input buttons. They will not display all data apart of resource file values. The same data will display properly using other html tags.
Create default mvc project > Add properties\resource.resx > add one entry to file (make sure value has more than one word) > set file properties Custom Tool= PublicResXFileCodeGenerator > change index.cshtml to this code > set to chrome browser > run project. Notice the input tag will not display all data from resource file properly but other html tags will.
resources.resx
<data name="updated_information_btn" xml:space="preserve">
<value>Word1 Word2 Word3</value>
</data>
index.cshtml
#using TestBnResourceFile.Properties
<div class="jumbotron">
<h2>Testing button value displayed</h2>
<p>
Button has problem displaying all words of resource file key.
</p>
</div>
<div class="row">
<h2>
<span>"Resources.updated_information_btn" value:</span> #Resources.updated_information_btn
</h2>
</div>
<div class="row">
<input type="submit" name="submitbtn1" value=#Resources.updated_information_btn class="submitbtn">
</div>
<div class="row">
<input type="submit" name="submitbtn2" value=#Resources.updated_information_btn>
</div>
<div class="row">
<input name="submitbtn3" value=#Resources.updated_information_btn>
</div>
<div class="row">
<input value=#Resources.updated_information_btn>
</div>
That's because you need to reference the string value in the following way:
<div class="row">
<input type="text" name="date" value="<%= ResourceValue %>" />
</div>
So, if you want to bind the resource value, you can assign it to a string var and then set it:
private string ResourceValue{ get; set; }
protected void Page_Load(object sender, EventArgs e)
{
this.ResourceValue = #Resources.resource_value;
}
Let me know if it helps, cheers.
Using double quotes around the variable resolved this issue.
#{
var myString = "worda wordb wordc";
}
<div class="row">
<input value="#myString">
</div>

Helper does not work (ASP.NET MVC )

I try to create a helper in my project.
This is my code:
#using VrBlog.Models;
#helper Render(Post post, System.Web.Mvc.HtmlHelper html, bool isAdmin, bool showComments)
{
<div class="postTitle">#post.Title</div>
<div class="postContainer">
<div class="postTabs">
<div class="dateTab">
<div class="month">#post.DateTime.Value.ToString("MMM").ToUpper()</div>
<div class="day">#post.DateTime.Value.ToString("dd")</div>
</div>
<div class="commentsTab">
#post.Comments.Count
</div>
</div>
<div class="postContent">
<div class ="postBody">#html.Raw(post.Body)</div>
<div class="tagList">
#foreach (Tag tag in post.Tags)
{
<span class="tag">#tag.Name</span>
}
</div>
<div class="linkList">
#{ string url = "http://www.mattblagden.com/posts/details/" + post.Id;}
</div>
</div>
</div>
if (showComments)
{
<div id="commentsContainer">
<a id ="comments"></a>
#foreach (Comment comment in post.Comments.OrderBy(x => x.DateTime))
{
<div class="comment">
<div class="commentName">
#if (!string.IsNullOrWhiteSpace(comment.Email))
{
#comment.Name
}
else
{
#comment.Name;
}
</div>
said:
<div class="commentBody">#html.Raw(html.Encode(comment.Body).Replace("\n", "<br/>"))</div>
<div class="commentTime">at #comment.DateTime.Value.ToString("HH:mm") on #comment.DateTime.Value.ToString("yyyy/MM/dd")</div>
</div>
}
<div id="commentEditor">
<div id="commentPromt">Leave a comment!</div>
<form action="#Href("~/Views/Posts/Comment/" + post.Id)" method="post">
<input type="text" id="commentNamePromt" name="name"/>Name (required)<br/>
<input type="text" id="commentEmailPromt" name="email" />Email (optional)<br/>
<textarea id="commentBodyInput" name="body" rows="10" cols="60"></textarea><br/>
<input type="submit" id="commentSubmitInput" name="submit" value="Submit!"/>
</form>
</div>
</div>
}
}
And after this, I try to call it from the view. My helper is in the AppCode folder inside the project.
I try to call it like this
#foreach (Post post in Model)
{
#PostHelper.Render(post,Html,isAdmin, false)
}
But I get the following error:
Error CS0103 The name 'PostHelper' does not exist in the current context
How I can fix this and why it not visible?
If you are calling the helper on the same page, try following:
#foreach (Post post in Model)
{
#Render(post, Html, isAdmin, false)
}
Change this
#foreach (Post post in Model)
{
#PostHelper.Render(post,Html,isAdmin, false)
}
to
#foreach (Post post in Model)
{
#Render(post,Html,isAdmin, false)
}
Rename your folder in App_Code
"We can define the #helper method outside of our view template, and enable it to be re-used across all of the view templates in our project.
We can accomplish this by saving our #helper methods within .cshtml/.vbhtml files that are placed within a \App_Code directory that you create at the root of a project."

How to send data from controller to viewmodel textbox

I'm working on a MVC-project for fun that takes data from a public API, shows it, and calculates it depending on user input; I'm nearing the end of my goal, but one issue I've been having trouble with for the past day is blocking my progress.
The issue is as follows:
I want to be able to take data from my Controller, and show on button-press in my view, inside a writable text-box (so the user can both auto-generate data by inputting username and fetching their data from the public API, or specify their own custom data).
View
#{
ViewBag.Title = "Herblore";
Enter you name #Html.TextBox("Name")
<input type="submit" id="SubmitName" value="Submit" />
<script type = "text/javascript">
$('#SubmitName').click(function() {
var url = "/Calculator/PlayerLevelMsg";
var name = $('#Name').val();
$.get(url, {
Brugername: name
}, function(data) {
$("#lData").html(data);
});
})
</script>
<div class="row">
<div class="col-md-12">
<div class="row">
<div class="col-md-3">
<h3 style="font-size: 24px">Current Level</h3>
</div>
<div class="col-md-3 col-md-pull-1">
<h3><output id="lData"/></h3>
</div>
<div class="col-md-3 col-md-pull-1">
<h3 style="font-size: 24px">Current EXP</h3>
</div>
<div class="col-md-3 col-md-pull-2">
<h3><input type="text" id="lData" /></h3>
</div>
</div>
</div>
</div>
My Javascript allows me to run this controller in my calculator controller class:
public string PlayerLevelMsg(string BrugerName) {
int Firemaking = 12;
var playerdata = new OS_Efficiency_Calculator.Controllers.PlayerSearchController();
string PlayerLevel = playerdata.GetLevel(BrugerName, Firemaking);
string moreLevel = "PlayerLevel = 85";
if (!String.IsNullOrEmpty(BrugerName))
return "Player Level" + " " + moreLevel;
else
return "Please enter your name.";
}
Which returns a string that holds a single value from the API - this shouldn't be important, in the name of reproducing my issue, commenting out the strings gathered from other classes in my project and using a basic text string will give you the same result as I'm getting, so if you wish to reproduce:
public string PlayerLevelMsg(string BrugerName) {
if (!String.IsNullOrEmpty(BrugerName))
return "Player Level = 99";
else
return "Please enter your name.";
}
My issue is as follows;
Using the above code, I can view the output of my controller just fine in the line "<output id="lData"/>" - however, this isn't a text-box, so the user can't edit it.
on the other hand, will not. This is likely because it's an input type, but I can't find a textbox-output type, nor am I experienced enough in MVC to fully understand why I can't just bind the textbox to the value through the ID, when that would work just fine in a general WPF application (where I've spent the most time practicing).
It seems to me that there should be some super-simple answer to this, and I hope you guys can help me, and that the semi-code provided is adequate; As said, the ideal solution is that in this picture:
Both of the values would be "85", but the text-box is adamant that it isn't supposed to change - Thanks so much for taking the time to look through!
#{
ViewBag.Title = "Herblore";
Enter you name #Html.TextBox("Name")
<input type="submit" id="SubmitName" value="Submit" />
< script type = "text/jscript" >
$('#SubmitName').click(function() {
var url = "/Calculator/PlayerLevelMsg";
var name = $('#Name').val();
$.get(url, {
Brugername: name
}, function(data) {
$("#lData").val(data);
});
})
< /script>
<div class="row">
<div class="col-md-12">
<div class="row">
<div class="col-md-3">
<h3 style="font-size: 24px">Current Level</h3>
</div>
<div class="col-md-3 col-md-pull-1">
<h3><input type="text" id="lData"/></h3>
</div>
<div class="col-md-3 col-md-pull-1">
<h3 style="font-size: 24px">Current EXP</h3>
</div>
<div class="col-md-3 col-md-pull-2">
<h3><input type="text" id="lData" /></h3>
</div>
</div>
</div>
</div>
You need to make lData an input field with the type text and in the JavaScript function set the value of the field to the data returned from the controller.
Please visit this site to learn more about the input field: http://www.w3schools.com/tags/tag_input.asp

Clicking class button on website

I have the following code for a webpage;
<div class="toolbox">
<div class="toolboxNest" align="left">
<div class="clear"> </div>
<div class="gap"> </div>
<label for="j_username">
<input id="j_username" type="text" name="j_username">
<div class="clear"> </div>
<div class="gap"> </div>
<label for="j_password">
<input id="j_password" type="password" name="j_password">
</div>
<div class="clear"></div>
<div class="gap"></div>
<div align="center">
<input class="formButtonGreen" type="submit" value="Login">
</div>
<div class="clear"></div>
<div class="gap"></div>
<div class="infoText">
I am able to click and enter text for the username and password, but I cant work out how to press the submit button, if it were just an element then that would be fine, but it's in a class and I'm unsure how to do it.
This is my code...
var x = webBrowser1.Document.All.GetElementsByName("j_username");
x[0].InnerText = "xxxxx";
var y = webBrowser1.Document.All.GetElementsByName("j_password");
y[0].InnerText = "xxxxxxxx";
//This part is not working....
var s = webBrowser1.Document.GetElementById("formButtonGreen");
s[0].InvokeMember("click");
Any one got any ideas?
Please note: I don't own the website so can't make changes to code behind webpage...
Thanks,
David
You should try using .Document.Body.GetElementsByTagName("input") which should help you get the button element and later you can use InvokeMember("click") on that element.
This will return a collection of matching elements, so if there are more than one you will have to identify it from the attributes by looping over it and then trigger for the one you need.
Instead of clicking the button, just submit the form itself.
jQuery wise, this is:
$("#my_form").submit();
Or you coulkd use any other means to select that Form. By class, parent element, tag name, ...

Categories

Resources