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?
Related
I'm pretty new to ASP.NET MVC, and I'm trying to figure out if I am able to bind data to my model property from within a loop.
Can I bind it maybe in the onClick event?
At the moment chosenTime is 1/01/0001 12:00:00 AM.
My code for the view is as follows:
#model ResSProject.Models.Sittings.SittingTimesVM
<div class="d-flex justify-content-center">
<div>#Model.RestaurantName</div>
<div class="px-5">#Model.Date.ToString("dd-MMMM-yyyy")</div>
<div>#Model.NumberOfGuests</div>
</div>
<form method="post" asp-action="Reservation">
#{TimeSpan T1 = new TimeSpan(0, 15, 0);
for (var increase = Model.SittingsStart.Subtract(T1); increase < Model.SittingsEnd;)
{
increase = increase.AddMinutes(15);
<input class="btn btn-primary w-50 mx-auto mt-3 " type="button" asp-for="ChosenTime" value="#increase.ToString("HH:mm tt")" />
}
}
<input type="submit" value="Submit" />
<input type="hidden" asp-for=NumberOfGuests />
<input type="hidden" asp-for=Date />
<input type="hidden" asp-for=RestaurantName />
<input type="hidden" asp-for=RestaurantId />
<input type="hidden" asp-for=SittingsStart />
<input type="hidden" asp-for=SittingsEnd />
</form>
My view model class is:
using ResSProject.Data;
using System.ComponentModel.DataAnnotations;
namespace ResSProject.Models.Sittings
{
public class SittingTimesVM
{
public DateTime Date { get; set; }
public int RestaurantId { get; set; }
public string RestaurantName { get; set; }
public int NumberOfGuests { get; set; }
public DateTime ChosenTime { get; set; }
public DateTime SittingsStart { get; set; }
public DateTime SittingsEnd { get; set; }
}
}
Button type element cannot be passed to backend. I think you need set a hidden input and use js to set the chosen time.
Besides, the value of the inputs are just time without date(value="#increase.ToString("HH:mm tt")"). When you post the value, it will bind time with current date to the property ChosenTime. If the SittingsStart and SittingsEnd are not the same date with current now, the chosen time will be not correct.
Whole working demo:
#model SittingTimesVM
<div class="d-flex justify-content-center">
<div>#Model.RestaurantName</div>
<div class="px-5">#Model.Date.ToString("dd-MMMM-yyyy")</div>
<div>#Model.NumberOfGuests</div>
</div>
<form method="post" asp-action="Reservation">
#{TimeSpan T1 = new TimeSpan(0, 15, 0);
for (var increase = Model.SittingsStart.Subtract(T1); increase < Model.SittingsEnd;)
{
increase = increase.AddMinutes(15);
<input class="btn btn-primary w-50 mx-auto mt-3 " type="button" asp-for="ChosenTime"
onclick="GetValue(this)" value="#increase.ToString("dd-MMMM-yyy HH:mm tt")" />
}
}
<input asp-for="ChosenTime" type="hidden"/>
<input type="submit" value="Submit" />
<input type="hidden" asp-for=NumberOfGuests />
<input type="hidden" asp-for=Date />
<input type="hidden" asp-for=RestaurantName />
<input type="hidden" asp-for=RestaurantId />
<input type="hidden" asp-for=SittingsStart />
<input type="hidden" asp-for=SittingsEnd />
</form>
#section Scripts
{
<script>
function GetValue(e)
{
$('input:hidden[name=ChosenTime]').val($(e).val())
}
</script>
}
Backend:
[HttpPost]
public void Reservation(SittingTimesVM model)
{
//do your stuff...
}
Another way I better suggest is to combine <input> elements to one <select> element, and it is no need add any onclick event to set value. It can bind to model property successfully upon you set asp-for="ChosenTime" for the <select> element.
#{
TimeSpan T1 = new TimeSpan(0, 15, 0);
<select type="button" asp-for="ChosenTime">
#for (var increase = Model.SittingsStart.Subtract(T1); increase < Model.SittingsEnd;)
{
increase = increase.AddMinutes(15);
<option value="#increase.ToString("dd-MMMM-yyy HH:mm tt")">#increase.ToString("HH:mm tt")</option>
}
</select>
}
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>
}
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.
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" />
}
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">