How to use hidden field values from view to controller in asp.net mvc 3 - asp.net-mvc-3

I have to pass hidden filed values to controller action. So I have tried in the following way, but I am getting null values.
I have tried both methods i.e formcollection and viewmodel concept
Controller
public ActionResult MapIcon()
{
Hidden hd = new Hidden();
return View(hd);
}
[HttpPost]
public ActionResult MapIcon(Hidden hidden)
{
var value=hidden.hiddevalue;//null
FormCollection col = new FormCollection();
var value = col["hidden1"];
// string value = mycontroler.ControlName;
return View(hidden);
}
View
#model SVGImageUpload.Models.Hidden
Razor view:#using (Html.BeginForm(new { id = "postform" }))
{
<input type="hidden" id="" value="7" name="hidden1" />
<input type="hidden" id="" value="7" name="hidden2"/>
<input type="submit" value="Match"/>
}
Viewmodel
public class Hidden
{
public string hiddevalue { get; set; }
}

Try this, In Razor view:
#using (Html.BeginForm(new { id = "postform" }))
{
#Html.HiddenFor(m=>m.hiddevalue)
<input type="submit" value="Match"/>
}

It seems to me like you are trying to get multiple values into the POST controller. In that case, and by your exam, the value from the hidden input is enough. In that case, you can setup your controller as so:
public ActionResult Index()
{
Hidden hd = new Hidden();
return View(hd);
}
[HttpPost]
public ActionResult Index(IEnumerable<string> hiddens)
{
foreach (var item in hiddens)
{
//do whatter with item
}
return View(new Hidden());
}
and as for your view, simple change it in order to bind to the same name "hiddens" as so:
#using (Html.BeginForm(new { id = "postform" }))
{
<input type="hidden" value="7" name="hiddens" />
<input type="hidden" value="2" name="hiddens" />
<input type="submit" value="Match" />
}
Hope this serves what you are looking forward to.

if your hidden value is static.Than try this
View
#using (Html.BeginForm(new { id = "postform" }))
{
#Html.HiddenFor(m=>m.hiddevalue)
<input type="hidden" id="" value="7" name="hidden1" />
<input type="hidden" id="" value="7" name="hidden2"/>
<input type="submit" value="Match"/>
}
Controller
[HttpPost]
public ActionResult MapIcon(Hidden hidden, string hidden1, string hidden2)
{
var hiddenvalue = hidden.hiddevalue;
var hiddenvalue1 = hidden1;
var hiddenvalue2 = hidden2;
var value=hidden.hiddevalue;//null
FormCollection col = new FormCollection();
var value = col["hidden1"];
// string value = mycontroler.ControlName;
return View(hidden);
}
Script
$(document).ready(function () {
$('#hiddevalue').val("Jaimin");
});

Related

Post HiddenField values to controller with model .net core 6

var tags = M.Chips.getInstance($('.chips')).chipsData;
var sendTags = JSON.stringify(tags, null, 2);
$('#Tags').val(sendTags);
with the above method I stored chips data to hiddenfileds with .onSubmit() method. But at controller level, couldn't get them.
I'am expecting hiddenfiled chips data at contorller level along with other model property.
But at controller level, couldn't get them.
If you want to get the hiddenfileds, I have a suggestion like below:
You can create a hidden input, and use name attribute to bind the data.
<input id="Tags" name="tags" type="hidden" />
The demo like below, you can refer to it:
Privacy view:
<form asp-action="Privacy" method="post">
<input id="Tags" name="tags" type="hidden" />
<input type="submit" value="submit"/>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<script>
var tags = {
'x1': 1,
'x2': 2
}
var sendTags = JSON.stringify(tags, null, 2);
$('#Tags').val(sendTags);
</script>
Controller:
public IActionResult Privacy()
{
return View();
}
[HttpPost]
public IActionResult Privacy( string tags )
{
return View();
}
result:

MVC3 form posting with value and file

I am new to MVC3 and I would like to create a form with input column and file upload.
The problem comes when I try to do both thing at the same time.
Here is my code
...
[HttpPost]
public ActionResult About(string inputStr)
{
string local = inputStr;
string[] word = inputStr.Split(':');
return View();
}
[HttpPost]
public ActionResult GetFile(string inputStr, HttpPostedFileBase file)
{
string filename = file.FileName;
return RedirectToAction("About");
}
These two are my controllers
#using (Html.BeginForm("GetFile", "Home", (new { inputStr = "111" }), FormMethod.Post, new { enctype = "multipart/form-data" })){
<div class="editor">
<input type="file" name="file" />
<input type="submit" value="OK" id="submitFile" class="testingSubmit"/>
</div>
}
This code works well for uploading files, and sending string "111" to the controller.
Here is another jQuery function
$('.testingSubmit').click(function () {
var totalString="";
$('.editor-field :input').each(function () {
alert($(this).val());
totalString += $(this).val().toString() + ":";
});
$('form').submit();
/* $.post("About", { inputStr: totalString}, function (data) {
});*/
});
Here, what I am trying to do is the get the user input and put it on string totalString.
I was able to post the totalString to the controller by using $.post
My questions are:
1. Am i on the right track? i.e. Is that possible to do those two tasks together with one post?
2. If not, what are the possible solution for this?
Thank you very much for your attention and hopefully this can be solved!
I think something like this should work:
#using (Html.BeginForm("GetFile", "Home", (new { inputStr = "111" }), FormMethod.Post, new { #id = "frmGetFile", enctype = "multipart/form-data" })){
<div class="editor">
<input type="file" name="file" />
<input type="hidden" name="totalString" id="totalString"/>
<input type="submit" value="OK" id="submitFile" onclick="submitGetFileForm()" class="testingSubmit"/>
</div>
}
JavaScript:
function submitGetFileForm(e)
{
e.preventDefault();
var total = //build your total string here
$('#totalString').val(total);
$('#frmGetFile').submit();
}
Controller:
[HttpPost]
public ActionResult GetFile(string totalString, HttpPostedFileBase file)
{
// your action logic..
}

MVC3 ViewBag Concept

This is View
ViewPage.cshtml
#using (Html.BeginForm("Display", "Home", FormMethod.Post))
{
<div>Name:<a>#ViewBag.st</a><br /></div>
<input type="submit" value="Submit" />
<br />
}
My jquery for editing #ViewBag.st
<script type="text/javascript">
$(document).ready(function () {
$('a').click(function () {
var textbox = $('<input id="Text1" type="text" size="100" />')
var oldText = $(this).text();
$(this).replaceWith(textbox);
textbox.blur(function () {
var newValue = $(this).val();
$(this).replaceWith($('<a>' + newValue + '</a>'));
});
textbox.val(oldText);
});
});
</script>
My controller methos is
public ActionResult Viewdetails()
{
User ur = new User();
ur.Name = "Danny";
ViewBag.st = ur.Name;
return View();
}
And my Model Class is
User.cs
public class User
{
//public string name { get; set; }
private string m_name = string.Empty;
public string Name
{
get
{
return m_name;
}
set
{
m_name = value;
}
}
}
After editing #ViewBag.title i want to store that value and pass to next View Can anyone suggest some ideas
I would recommend you to not use ViewBag at all.
So in your javascript you could give your textbox the same name as your User model property (Name):
var textbox = $('<input type="text" size="100" name="Name" />');
and then have the 2 controller actions (GET and POST):
public ActionResult Viewdetails()
{
User ur = new User();
ur.Name = "Danny";
return View(ur);
}
[HttpPost]
public ActionResult Display(User model)
{
return View(model);
}
And inside Viewdetails.cshtml:
#model User
#using (Html.BeginForm("Display", "Home", FormMethod.Post))
{
<div>Name: <a>#Model.Name</a><br /></div>
<input type="submit" value="Submit" />
<br />
}
And inside the Display.cshtml:
#model User
<div>You have selected: #Model.Name</div>

What are the parameters need to be put inside a Html.BeginForm()

I have the following view:
<h2>enter the student name to search</h2>
#using (Html.BeginForm())
{
<label>Search:</label>
<input type="text" name="searchString" />
<input type="submit" name="submit" />
}
and the view corresponds to following controller
namespace Searching.Controllers
{
public class SearchController : Controller
//somecode
[HttpPost]
public ActionResult SearchIndex(FormCollection formCollection)
{
string searchString=formCollection["searchString"];
var search = from m in db.Searches select m;
if (!String.IsNullOrEmpty(searchString))
{
search = search.Where(s => s.Name.Contains(searchString));
}
return View(search);
}
}
}
the controller name is SearchController.cs and the name of the method to pass form is SearchIndex.
What should be the parameter inside the Html.BeginForm().I am a begginer.
Html.BeginForm("SearchIndex", "Search", .....)
You should read this: http://msdn.microsoft.com/en-us/library/dd410596.aspx

Passing input value to action (ASP.Net MVC 3)

I have code in View:
#using (Html.BeginForm("MyAction", "MyController")
{
<input type="text" id="txt" />
<input type="image" src="/button_save.gif" alt="" />
}
How can I pass value of txt to my controller:
[HttpPost]
public ActionResult MyAction(string text)
{
//TODO something with text and return value...
}
Give your input a name and make sure it matches the action's parameter.
<input type="text" id="txt" name="txt" />
[HttpPost]
public ActionResult MyAction(string txt)
Add an input button inside of your form so you can submit it
<input type=submit />
In your controller you have three basic ways of getting this data
1. Get it as a parameter with the same name of your control
public ActionResult Index(string text)
{
}
OR
public ActionResult Index(FormsCollection collection)
{
//name your inputs something other than text of course : )
var value = collection["text"]
}
OR
public ActionResult Index(SomeModel model)
{
var yourTextVar = model.FormValue; //assuming your textbox was inappropriately named FormValue
}
I modified Microsoft MVC study "Movie" app by adding this code:
#*Index.cshtml*#
#using (Html.BeginForm("AddSingleMovie", "Movies"))
{
<br />
<span>please input name of the movie for quick adding: </span>
<input type="text" id="txt" name="Title" />
<input type="submit" />
}
//MoviesController.cs
[HttpPost]
public ActionResult AddSingleMovie(string Title)
{
var movie = new Movie();
movie.Title = Title;
movie.ReleaseDate = DateTime.Today;
movie.Genre = "unknown";
movie.Price = 3;
movie.Rating = "PG";
if (ModelState.IsValid)
{
db.Movies.Add(movie);
db.SaveChanges();
return RedirectToAction("Index");
}
else
{
return RedirectToAction("Index");
}
}

Resources