I'm using VSCode in Mac creating a MVC.
The parameter "new { problem = #Model }" shouldn't pass the values from the view to the controller?
View code:
#model ProblemsV4.Models.ProblemModel
<h2>New Solution</h2>
#using(Html.BeginForm("SaveSolution", "Problem", new { problem = #Model }, FormMethod.Get))
{
<label>Solution: </label>
<input type="text" name="solution" /><br/><br/>
<input type="submit" value="Save"/>
}
Controller code:
public IActionResult SaveSolution(ProblemModel problemModel, string solution)
{
SolutionModel model = new SolutionModel();
model.Solution = solution;
ProblemBC bc = new ProblemBC();
bc.AddSolution(model);
List<ProblemModel> models = bc.ListAll();
ViewBag.Message = "Solução inserida com sucesso";
return View("Index", models);
}
You should receive a HttpPost at the controller and send the parameters of the model with a HiddenFor, a type of
HtmlHelper that doesn't shows any input.
The BeginForm method has many signatures, one of them contains the Object routeValues, that is an object that contains the parameters for a route. You don't need to use that,
you can use other signature like this: https://msdn.microsoft.com/en-us/library/dd460344(v=vs.118).aspx
First step:
[HttpPost]
public IActionResult SaveSolution(ProblemModel problemModel, string solution)
{
SolutionModel model = new SolutionModel();
model.Solution = solution;
ProblemBC bc = new ProblemBC();
bc.AddSolution(model);
List<ProblemModel> models = bc.ListAll();
ViewBag.Message = "Solução inserida com sucesso";
return View("Index", models);
}
Second step:
#model ProblemsV4.Models.ProblemModel
<h2>New Solution</h2>
#using(Html.BeginForm("SaveSolution", "Problem", FormMethod.Post))
{
<label>Solution: </label>
<input type="text" name="solution" /><br/><br/>
#Html.HiddenFor(model => model.Problem)
#Html.HiddenFor(model => model.Description)
#Html.HiddenFor(model => model.ID)
<input type="submit" value="Save"/>
}
Related
I have my code as below.Can somebody help me in this I am not able to send model to view.
ViewModel class
IEnumerable<CarList> MyCarPositions.
In my view.
#model TestMVC.ViewModel.
foreach (var item in Model.MyCarPositions)
{
#Html.TextBoxFor(x => item.Name)
#Html.TextBoxFor(x => item.Brand)
}
My Controller
ViewModel carviewmodel = new carviewmodel();
[HttpGet]
public ActionResult Index()
{
carviewmodel.MyCarPositions = repository.GetCarPositions();
return View(carviewmodel);
}
[HttpPost]
public ActionResult Index(ViewModel carvmodel)
{
// Here in httppost carvmodel comes as null.
}
Your foreach loop is generating duplicate name attributes without indexers which cannot be bound to your model. Its also generating invalid html because of the duplicate id attributes. You need to generate the collection using a custum EditorTemplate for the type in your collection, or use a for loop.
You have not shown you models, but property MyCarPositions needs to be List<T> if using the for loop option.
#model TestMVC.ViewModel
#using Html.BeginForm())
{
for (int i = 0; i < Model.MyCarPositions.Count, i++)
{
#Html.TextBoxFor(x => x.MyCarPositions[i].Name)
#Html.TextBoxFor(x => x.MyCarPositions[i].Brand)
}
<input type="submit" .. />
}
This will generate the correct name attributes necessary for binding
<input type="text" name="MyCarPositions[0].Name" .... />
<input type="text" name="MyCarPositions[1].Name" .... />
<input type="text" name="MyCarPositions[2].Name" .... />
Side note: You should be initializing the model inside the controller method
// ViewModel carviewmodel = new carviewmodel(); remove this
[HttpGet]
public ActionResult Index()
{
ViewModel carviewmodel = new carviewmodel(); // initialize it here
carviewmodel.MyCarPositions = repository.GetCarPositions();
return View(carviewmodel);
}
Right away I can see a syntax error that might be causing your issue.
foreach (var item in Model.MyCarPositions)
{
#Html.TextBoxFor(x => item Name <--- missing a '.' and a closing bracket
#Html.TextBoxFor(x=>item.Brand)
}
Should be:
foreach (var item in Model.MyCarPositions)
{
#Html.TextBoxFor(x =>item.Name)
#Html.TextBoxFor(x=>item.Brand)
}
I am doing work on form where user can enter a customer record....View is scaffold with Create controller.
On 'Create' View, user can enter 'engineNo' to check its details which passes to another action "CheckRecord",,it can be seen from view...
<form>
<input type="text" id="enginNo" />
<input type="button" value="search" id="btnSearch" />
</form>
#using (Html.BeginForm("Index","Home",FormMethod.Get))
{
#Html.AntiForgeryToken()
<div id="info">
#{Html.RenderAction("CheckRecord","Sales");}
</div>
some create fields
}
The Create and "CheckRecord" actions are,,
public ActionResult Create()
{
ViewBag.CustomerId = new SelectList(db.CustomersDMs, "CustomerId", "Name");
ViewBag.SMClientBranchId = new SelectList(db.SMClientBranchesDMs, "SMClientId", "Name");
ViewBag.EngineNumber = new SelectList(db.StockDMs, "EngineNumber", "ChasisNumber");
return View();
}
public ActionResult CheckRecord(string enginNo)
{
var results = db.StockDMs.Where(c=>c.EngineNumber ==enginNo);
return PartialView("_part",results);
}
And my partialview,,,
#model IEnumerable<SM.CRM.AutosLoan.Models.Core.DomainModels.StockDM>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.AutoCompanyBrand.Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.SMClientBranch.Name)
</td>
}
My problem is, the partial view is rendered correctly but the Model of partial view doesn't have value,,,Why is that, i am doing something wrong...Please help,,,Thanks for your time
(Posting this as answer since I mentioned it in comments and that's not the correct place)
Your action CheckRecord(string enginNo) takes an argument of enginNo, but you're calling it without any argument. That in turn means your db lookup will most likely not return any results, unless you get results on..
var results = db.StockDMs.Where(c => c.EngineNumber == null);
Make sure the action gets a valid argument, for example:
#{ Html.RenderAction("CheckRecord", "Sales", new { enginNo = "abc123" }); }
I'm new to ASP.NET MVC. I'm trying to update model on button click with no success: every time I push the button an HttpGet controller method is invoked.
Here is my markup
#model DataInterface.Model.Entry
<button onclick="location.href='#Url.Action("Survey")'">Finish survey</button>
Here is Controller code
[HttpGet]
public ActionResult Survey()
{
var entry = new Entry();
return View(entry);
}
[HttpPost]
public ActionResult Survey(Entry newEntry)
{
// save newEntry to database
}
When I click button HttpGet method is invoked. Why?
It is bad to be a rookie)
Thanks to all!
If you access a URL without explicitly specifying the HTTP method, ASP.NET MVC will assume a GET request. To change this, you can add a form and send it:
#using (Html.BeginForm("Survey", "Controller", FormMethod.Post))
{
<input type="submit" value="Finish survey" />
}
If you do this, your POST method will be invoked. The Entry parameter, however, will be empty, since you do not specify any values to send along with the request. The easiest way to do so is by specifying input fields, e.g. text inputs, dropdown, checkboxes etc.
#using (Html.BeginForm("Survey", "Controller", FormMethod.Post))
{
#Html.TextBoxFor(m => m.Title)
<input type="submit" value="Finish survey" />
}
If you have the object stored on the server somewhere and only want to finish it off by writing it into the database or changing its status, you could pass the Id of the object (or some temporary Id) along the post request and make the controller method work only with the Id:
#using (Html.BeginForm("Survey", "Controller", FormMethod.Post))
{
#Html.HiddenFor(m => m.Id)
<input type="submit" value="Finish survey" />
}
[HttpPost]
public ActionResult Survey(Entry newEntry)
{
// newEntry.Id will be set here
}
#using (Html.BeginForm("Survey", "<ControllerName>", FormMethod.Post))
{
<input type="submit" value="Finish survey" />
}
you must declare your form
#model DataInterface.Model.Entry
#using (Html.BeginForm("action", "Controlleur", FormMethod.Post, new {#class = "form", id = "RequestForm" }))
{
<input type="submit" value="Finish survey" />
}
When I have a DropDownList that relevant to Model of view like this:
#Html.DropDownListFor(model => model.Group.Name, selectList)
I Can retrieve Values in controller as follow:
string SelectedGroupName = collection.GetValue("Group.Name").AttemptedValue;
But now I have a DropDownList that not relevant to model but I need the value of that, this is my new DropDown:
#Html.DropDownList("DDName", selectList)
How can I retrieve the selected value of that in controller? is there any hiddenfield or other thing to pass value from view to controller?
Edit
This is my view:
#model PhoneBook.Models.Numbers
#{
ViewBag.Title = "Delete";
}
<h2>
Move And Delete</h2>
<fieldset>
<legend>Label of Numbers</legend>
<div class="display-label">
Delete Label And Move All Numbers with: #Html.DisplayFor(model =>
model.Title)</div>
<div class="display-field">
To #Html.DropDownList("DDName", selectlist)
</div>
</fieldset>
#using (Html.BeginForm()) {
<p>
<input type="submit" value="Move Numbers And Delete Label" name="MDbtn" />
</p>
}
This is my Controller:
[HttpPost]
public ActionResult Delete(int id, FormCollection collection) {
var result = Request["DDName"];
//Use result
return RedirectToAction("Index");
}
but result set to null, why?
I think this must be work:
[HttpPost]
public ActionResult Delete(int id, FormCollection collection)
{
var dd = collection.GetValue("DDName");
.....
}
I think all you have to do is
In your view:
put #using (Html.BeginForm()) { above the <fieldset>, so the #Html.DropDownList("DDName", selectlist) is inside it.
In your controller:
public ActionResult Delete(int id, FormCollection collection, string DDName)
{ [...] }
And I'm fairly sure MVC3 will automagically give you the selected value as parameter to your controller.
If that does not work, try object DDName in your controller instead.
Think the ddl has to be in your form if you want to pass the value in through the form collection
Your problem is, that your dropdown isn't contained inside the form in your view.
You have to put it after BeginForm:
#using (Html.BeginForm()) {
<div class="display-field">
To #Html.DropDownList("DDName", selectlist)
</div>
<p>
<input type="submit" value="Move Numbers And Delete Label" name="MDbtn" />
</p>
}
Then you have can use FormCollection or a designated parameter. The default Modelbinder will work with both approaches:
ActionResult Action (FormCollection collection, string DDName)
You can easily check those issues with fiddler.
Can someone help me with model validation on a HttpPostedFileWrapper object?
Model:
[Required(AllowEmptyStrings = false)]
public HttpPostedFileWrapper BlahFile { get; set; }
Controller:
[HttpPost]
public ActionResult LoadBlahData(BlahModel blahModel)
{
if (!ModelState.IsValid)
return RedirectToAction("Index");
}
cshtml:
#using (Html.BeginForm("LoadBlahData", "Admin", FormMethod.Post, new { #class = "blahhForm", enctype = "multipart/form-data", id = "uploadBlah" }))
{
<fieldset>
<legend>Upload Blah Information</legend>
#Html.LabelFor(x=>x.BlahFile, "Upload Blah file:")
<input size="26" class="uploader" type="file" name="BlahFile" />
<p><input class="ttButton" type="submit" value="Load Stuff" /></p>
</fieldset>
}
Problem:
Cannot see the "data-val*" attributes being added to the html.
Does not set the unobtrusive validation off (red border on input box)
Notes:
Other items in the Model are working fine with validation, its only the <input type="file"/> that seems to be having problems.
Comes into the action method fine - (i.e - i can access the InputStream if i want).
All scripts are referenced correctly (its working on normal text input's)
Thanks in advance,
Just for anyone else coming across this question you can also do this -
<%: Html.TextBoxFor(x => x.BlahFile, new { type = "file" }) %>
you have not added any data attributes to your element. Add it like,
<input data-pk="1" size="26" class="uploader" type="file" name="BlahFile" />
and there is no support of validation of <input type="file"