Razor change label on HttpPost - c#

new to razor, I have a label and a button
<form action="" method="post" enctype="multipart/form-data">
<label name="Mylabel">Test + #ViewData["MyLabelUpdate"]</label>
<input type="submit" id="MyBtn" Value="Change Label">
Now in the controller I have:
[HttpPost]
public ActionResult Index()
{
return RedirectToAction("Index");
}
How do I get the label to change using the actionresult method? Thanks

You have to make your controller look like this:
[HttpPost]
public ActionResult Index()
{
ViewBag.MyLabelUpdate = "whatever";
return RedirectToAction("Index");
}
And, your view:
<form action="" method="post" enctype="multipart/form-data">
<label name="Mylabel">Test + #ViewBag.MyLabelUpdate</label>
<input type="submit" id="MyBtn" Value="Change Label">

Related

How do i pass two text inputs into ActionResult method

I've been working on a large project for a few days. Its all finished except for this part. I'm trying to pass 2 text boxes into my home controller method.
I've tried to use "model." and "item." but with no luck. it always passes null
<p>
#Html.ActionLink("Display", "Home", FormMethod.Post)
</p>
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div>
Month:<input type="text" name="Month" id="MonthInput">
Extra:<input type="text" name="Extra" id="ExtraInput"><br /><br />
<input type="submit" value="Add" class="btn btn-default"> <br> <br>
</div>
}
and in the home controller is
[HttpPost]
public ActionResult Display(String Month, String Extra)
{
if (ModelState.IsValid)
{
...
return RedirectToAction("Index");
}
return view;
}
I want it to pass whatever value is typed in the textboxes when the "Add" Button is clicked but it gives null
try this,
In View
#using (Html.BeginForm("Display", "Home", FormMethod.Post))
{
#Html.AntiForgeryToken()
<div>
Month:<input type="text" name="Month" id="MonthInput">
Extra:<input type="text" name="Extra" id="ExtraInput"><br /><br />
<input type="submit" value="Add" class="btn btn-default"> <br> <br>
</div>
}
In Controller,
[HttpPost]
public ActionResult Display(SomeClass someClass)
{
if (ModelState.IsValid)
{
...
return RedirectToAction("Index");
}
return view;
}
Create Normal class name SomeClass, name as per your requirment,
public class SomeClass{
public string Month {get;set;}
public string Extra {get;set;}
}
the name attribute of inputs should match to properties of SomeClass, it will map automatically, you then can access the value by using someClass object
someClass.Month and someClass.Extra
In order to correctly map to the action's parameters, you must use the name attribute.
#using (Html.BeginForm("Display", "Home"))
{
#Html.AntiForgeryToken()
<div>
Month:<input type="text" name="Month" id="MonthInput">
Extra:<input type="text" name="Extra" id="ExtraInput"><br /><br />
<input type="submit" value="Add" class="btn btn-default">
</div>
}
Totally unrelated, but it seems you have 2 nested forms and one of them POSTs to a php page. Is that desired?
Also, remove the following code because it does not make any sense:
<p>
#Html.ActionLink("Display", "Home", FormMethod.Post)
</p>
To add on to the previous answer, I would suggest accepting the form as a FormCollection in the controller, because the form does not seem to be tied to the model:
[HttpPost]
public ActionResult Display(FormCollection collection)
{
if (ModelState.IsValid)
{
string month = collection["Month"];
string extra = collection["Extra"];
return RedirectToAction("Index");
}
return View();
}
namespace Your.Models
{
public class DisplayModel
{
public string Month {get;set;}
public string Extra {get;set;}
}
}
[HttpPost]
public ActionResult Display(DisplayModel model)
{
if (ModelState.IsValid)
{
string month = model.Month;
string extra = model.Extra;
return RedirectToAction("Index");
}
return View();
}
#using Your.Models
#model DisplayModel
#using (Html.BeginForm("Display", "Home", new { id="displayview" }))
{
#Html.AntiForgeryToken()
<div>
Month:<input type="text" name="Month" id="MonthInput">
Extra:<input type="text" name="Extra" id="ExtraInput"><br /><br />
<input type="submit" value="Add" class="btn btn-default">
</div>
}

Get text file as input in c# mvc

I want to get the text file as input but I am getting only null value...
public ActionResult add_content()
{
return View();
}
[HttpPost]
public ActionResult add_content(HttpPostedFileBase d)
{
return View();
}
HTML:
<form action="http://localhost:49416/g1/add_content" method="post">
<input type="file" name="d" />
<input type="submit" name=" add content" />
</form>
I have included the screenshot with breakpoint
You can try this:
Controller:
[HttpPost]
public ActionResult Upload(HttpPostedFileBase file)
{
return View();
}
View:
#using (Html.BeginForm("Upload", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<label for="file"> Filename:</label>
<input type="file" name="file" id="file" />
<input type="submit" />
}
You were missing the enctype = "multipart/form-data"
Also it is important to note that input name must be the same in controller.

Html.Hidden() generating blank values in HTML

I have a form on my view:
#using (Html.BeginForm("ClearData", "MemberPass", FormMethod.Post))
{
<div>
#foreach (var property in ViewData.ModelMetadata.Properties)
{
#Html.Hidden(property.PropertyName, property.Model)
}
</div>
<button>Clear</button>
}
and the following action methods:
public ActionResult Index()
{
MemberPassInfoViewModel memberPassInfoViewModel = new ServiceUtilities().GetEventDetails(DateTime.Now.Date);
return View("Index", memberPassInfoViewModel);
}
public ActionResult GetMemberPassInfo(MemberPassInfoViewModel currentMemberPassInfoValues)
{
MemberPassInfoViewModel updatedMemberPassInfoViewModel = new ServiceUtilities().GetMemberPassInfoViewModel(currentMemberPassInfoValues);
return View("Index", updatedMemberPassInfoViewModel);
}
public ActionResult ClearData(MemberPassInfoViewModel currentMemberPassInfoValues)
{
MemberPassInfoViewModel updatedMemberPassInfoViewModel = new ServiceUtilities().ClearData(currentMemberPassInfoValues);
return View("Index", currentMemberPassInfoValues);
}
In debug I can see that the model's two properties are present. However when I view the generated HTML, one of the properties has a blank value:
<input id="SearchCode" name="SearchCode" type="hidden" value="23" />
<input id="FullName" name="FullName" type="hidden" value="" />
I noticed that this could be fixed by replacing:
#Html.Hidden(property.PropertyName, property.Model)
with:
<input type="hidden" value="#property.Model" name="#property.PropertyName" />
Which generates:
<input type="hidden" value="23" name="SearchCode" />
<input type="hidden" value="Alex Robert" name="FullName" />
Why does the #Html.Hidden() method not work, but explicitly writing the <input> tag does work?

File upload in C# with ASP.NET MVC without razor

I am trying to submit a form containing a file upload using c# ASP MVC with Entity. My problem is that the file is always null.
The view :
#model Com.Work.With.Me.Models.ObjVM
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
<form id="formObj" action="AddMe">
<input type="file" id="objPdfFile" name="Obj.PdfFile" />
</select>
<input type="text" id="objName" name="Obj.Name" />
</form>
The viewmodel :
public class ObjVM
{
public string Name{ get; set; }
public HttpPostedFileBase PdfFile{ get; set; }
public ObjVM()
{
}
}
The controller :
public ActionResult AddMe(ObjVM obj)
{
//her obj.Name is fine
//but obj.PdfFile is null
return View();
}
Any ideas?
Thanks to #DiskJunky, I corrected my form adding method="post" enctype="multipart/form-data":
<form id="formObj" action="AddMe" method="post" enctype="multipart/form-data">
<input type="file" id="objPdfFile" name="Obj.PdfFile" />
</select>
<input type="text" id="objName" name="Obj.Name" />
</form>
And my controller adding [HttpPost] :
[HttpPost]
public ActionResult AddMe(ObjVM obj)
{
//obj.PdfFile is not null anymore !
return View();
}
Add Your Ui to this Code `enctype = "multipart/form-data"` Code
#using (Html.BeginForm("Action Name", "Control Name", null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" id="objPdfFile" value="#Model.PdfFile" name="Obj.PdfFile" />
</select>
<input type="text" id="objName" value="#Model.Name" name="Obj.Name" />
}

Post form from modal partial view to a different controller with a routeprefix attribute

I have a partial view that has a form - I launch this partial from the Home controller.
#using (Html.BeginForm("CloudContent","Files", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div id="container"></div>
<input type="submit" name="PostMe" title="Submit" value="Submit Form" />
#Html.HiddenFor(m => m.MyProperty);
#Html.HiddenFor(m => m.SelectedIds);
}
I want to post this form to the Files controller, however the files controller has a route prefix:
[RoutePrefix("sth/api/v1/files")]
Which results in my form action being empty.
It doesn't seem to be added to named routes, so I cannot use the beginrouteform. Also, I have little influence over the application, so it's best handled in my form or partial view...
The method that I want to post to in the files controller:
[HttpPost]
[Route("getcloudcontent")]
public List<ConnectedFile> CloudContent(CloudFilesModel model)
{
//do magic
}
UPDATE:
Here is another example which includes partial view / child action:
HomeController.cs:
using System.Web.Mvc;
public class HomeController : Controller
{
[Route("~/")]
public ActionResult Index()
{
return View();
}
[Route("ModalContent")]
[ChildActionOnly]
public ActionResult ModalContent()
{
return View();
}
}
FilesController.cs:
using System.Web.Mvc;
[RoutePrefix("sth/api/v1/files")]
public class FilesController : Controller
{
[HttpPost]
[Route("getcloudcontent")]
public ActionResult CloudContent(string model)
{
return Content("test");
}
}
Views\Home\Index.cshtml:
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
Show modal
</button>
<div class="modal fade" id="myModal" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body">
#Html.Action("ModalContent")
</div>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</body>
</html>
Views\Home\ModalContent.cshtml:
#using (Html.BeginForm("CloudContent", "Files", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div id="container"></div>
<input type="submit" name="PostMe" title="Submit" value="Submit Form" />
}
Global.asax.cs:
using System.Web.Mvc;
using System.Web.Routing;
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
RouteTable.Routes.MapMvcAttributeRoutes();
}
}
And the result HTML:
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
Show modal
</button>
<div class="modal fade" id="myModal" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body">
<form action="/sth/api/v1/files/getcloudcontent" enctype="multipart/form-data" method="post">
<div id="container"></div>
<input type="submit" name="PostMe" title="Submit" value="Submit Form" />
</form>
</div>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</body>
</html>
There is something different that is wrong with your code. I ran your code and it worked without any issues.
Here is the code:
FilesController.cs:
using System.Web.Mvc;
namespace Controllers
{
[RoutePrefix("sth/api/v1/files")]
public class FilesController : Controller
{
[Route("")]
public ActionResult Index()
{
return View();
}
[HttpPost]
[Route("getcloudcontent")]
public ActionResult CloudContent(string model)
{
return Content("test");
}
}
}
Index.cshtml:
#using (Html.BeginForm("CloudContent", "Files", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div id="container"></div>
<input type="submit" name="PostMe" title="Submit" value="Submit Form" />
}
Global.asax.cs:
using System.Web.Mvc;
using System.Web.Routing;
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
RouteTable.Routes.MapMvcAttributeRoutes();
}
}
When I go to http://localhost/sth/api/v1/files I see the following HTML:
<!DOCTYPE html>
<html>
<head><title>Test</title></head>
<body>
<form action="/sth/api/v1/files/getcloudcontent" enctype="multipart/form-data" method="post"> <div id="container"></div>
<input type="submit" name="PostMe" title="Submit" value="Submit Form" />
</form>
</body>
</html>
Notice that form action has the correct URL which includes prefix.

Categories

Resources