Sunday, 15 April 2012

Passing data from cshtml view to database when clicking submit. ASP.NET MVC 5 -



Passing data from cshtml view to database when clicking submit. ASP.NET MVC 5 -

i want store path of file, in database, uploaded user. not sure how accomplish correctly way attempting involves file name beingness captured within method controller , beingness passed view via tempdata[];

the cshtml variables this:

var filename = tempdata["filename"]; var filepath = string.format("{0:mm-yyyy}/", datetime.now); var filepath = filepath + filename;

where "filename" has captured file name passed controller , "filepath" uppercase p current month , year concatenate "filename" in "filepath" lowercase p.

then using html helper this:

@html.textboxfor(model => model.filelnq, new { @value = filepath})

... store path in database.

there 2 problems:

one, helper shows file path want store in text box helper designed for, utilize helper sends info database.

two, since method grabs file name called after submit selected, "filepath" still month , year, finish string current_month_and_year/filename needs sent don't know how done. naturally using next select file uploaded:

<input type="file" id="filetoupload" name="file" />

using current method have submit file twice before right file path stored, , has side effect of showing file path storing in database.

i'm sure there must simpler way of accomplishing using htlm helper not aware of.

this took little while head around after moving on other aspects of project came , solved it. suspected there in-fact helper solved part of me. instead of using (c#):

<div id ="filename" class="editor-field"> @html.textboxfor(model => model.filelnq, new { @value = filepath}) </div>

which displays text field.

i used (vb):

<div id ="filename" class="editor-field"> @html.hiddenfor(function(model) model.filelnq) </div>

which hides text field. solved problem 1 in question posed. notice no longer pre-loading info html helper no longer needed , testing purposes anyways.

to solve sec part of problem abandoned thought of passing info controller view using (c#):

var filename = tempdata["filename"];

since, wrote in original question, passing info on submit button selection. instead using java within jquery function:

$(document).ready(function () { // "file select button" alter event $('#filetoupload').change(function () { // <input> id on alter event var filename = $('input[type=file]').val().split('\\').pop(); // file name , parse remove "c:\fakepath\" string. $("#filename > input").val(filename); // store "filename" <div> id "filename" }); });

was needed pull file name needed file select input tag:

<input type="file" id="filetoupload" name="file" />

which main problem.

hope helps might come across similar.

database asp.net-mvc-5 html-helper submit-button

No comments:

Post a Comment