MVC Controller doesn't control the view - asp.net

I have a button called in my view like this one
<form>
<div class="field">
<label for="product-name">Product Name</label><br />
#Html.TextBoxFor(x=> x.Name, new {id="product-name",required="required"}) <br />
<span class="helper">Give your product a name.</span>
#ViewBag.mm
</div>
<div class="field">
<label for="product-variety">Product Variety</label><br />
<input id="product-variety" type="text" /><br />
<span class="helper">Optionally, give your product a variety.</span>
</div>
<div class="field">
<label for="product-description">Description</label><br />
<input id="product-description" type="text" /><br />
<span class="helper">Add an optional description to help identify your product.</span>
</div>
<div class="field">
<label for="product-comments">Comments</label><br />
<textarea id="product-comments"></textarea><br />
<span class="helper">Add any further information or comments relating to this product.</span>
</div>
<input id="product-save-return" type="submit" value="Add Product and Return to Products" class="green-button" /> <input id="product-save-associate" type="button" value="Add Product and Associate with Sites" class="green-button" /> <input id="file-cancel" type="button" value="Cancel" class="green-button" />
and in the controller I have a function like this
[HttpPost]
public ActionResult Add(Product product)
{
SupplierContext db = new SupplierContext();
Response.Write("Inside");
ViewBag.mm = "xyz";
if (ModelState.IsValid)
{
product.Key = Guid.NewGuid();
product.Type = ProductType.Chemical;
product.Status = EntityStatus.Default;
db.Products.Add(product);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(product);
}
but when i click the button, the function in the controller doesn't work.

Is your button within a form/action? In Razor it would simply just be a case of adding your form elements within the following syntax:
#using (Html.BeginForm())
{
<div class="field">
<label for="product-name">Product Name</label><br />
#Html.TextBoxFor(x=> x.Name, new {id="product-name",required="required"}) <br />
<span class="helper">Give your product a name.</span>
#ViewBag.mm
</div>
<div class="field">
<label for="product-variety">Product Variety</label><br />
<input id="product-variety" type="text" /><br />
<span class="helper">Optionally, give your product a variety.</span>
</div>
<div class="field">
<label for="product-description">Description</label><br />
<input id="product-description" type="text" /><br />
<span class="helper">Add an optional description to help identify your product.</span>
</div>
<div class="field">
<label for="product-comments">Comments</label><br />
<textarea id="product-comments"></textarea><br />
<span class="helper">Add any further information or comments relating to this product.</span>
</div>
<input id="product-save-associate" type="button" value="Add Product and Associate with Sites" class="green-button" /> <input id="file-cancel" type="button" value="Cancel" class="green-button" />
<input id="product-save-return" type="submit" value="Add Product and Return to Products" class="green-button" />
}
This could be why it is not picking up the function in the controller. You could do this in javscript too with an AJAX post...

You can also use an ActionLink:
#Html.ActionLink("Add Product and Return to Products", "Add", "YourController")
Though this will be a link instead of a button.

Related

The model item passed into the ViewDataDictionary is of type but this ViewDataDictionary instance requires a model item of type 'System.Collections

When I Want to post my Form in Asp.net.Mvc I get this Problem How can i fix that
InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'FormValidation.Models.Entity.Personel', but this ViewDataDictionary instance requires a model item of type 'System.Collections.Generic.IEnumerable`1[FormValidation.Models.Entity.Personel]'.
Controller
[HttpGet]
public IActionResult Index()
{
return View();
}
[HttpPost]
public IActionResult Post(Personel personel)
{
return View(personel);
}
form index
#model FormValidation.Models.Entity.Personel
<form method="post" asp-action="Post" asp-controller="Home">
<div class="container">
<div class="row">
<div class="form-group">
<label for="Name" class="form-control" placeholder="Enter Your Name">Name</label>
<input type="text" class="form-control" asp-for="Name" />
</div>
<br />
<div class="form-group">
<label for="Surname" class="form-control" placeholder="Enter Your Surname">Surname</label>
<input type="text" class="form-control" placeholder="Enter Your surname" asp-for="Surname" />
</div>
<br />
<br />
<div class="form-group">
<label for="City">City select</label>
<select class="form-control" asp-for="City">
<option></option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
</div>
<br />
<br />
<br />
<div class="form-group">
<label for="Age" class="form-control" placeholder="Enter Your Age">Age</label>
<input type="number" class="form-control" placeholder="Enter Your Age" asp-for="Age" />
</div>
<br />
<div class="form-group">
<label for="Adress">Adres</label>
<textarea class="form-control" rows="3" asp-for="Adress">Adres</textarea>
</div>
</div>
</div>
<br />
<input type="submit" name="submit" value="submit" class="btn btn-primary" />
</form>
Post index
#model IEnumerable<FormValidation.Models.Entity.Personel>
#foreach (var item in Model)
{
<p>#item.Name</p>
<p>#item.Surname</p>
<p>#item.Age</p>
<p>#item.City</p>
}
just as the error indicates,you passed the single personel model to render the view with return View(personel);
However,the View requires IEnumerable<Personel>
If you just want to show the details of the model you just created ,modify your view :
#model FormValidation.Models.Entity.Personel
.......
<p>#Model.Name</p>
<p>#Model.Surname</p>
<p>#Model.Age</p>
<p>#Model.City</p>

How to change the form controls id and name in Razor Pages

I have a contact Razor page backed by a ViewModel called ContactViewModel. The html generated is below.
<form method="post">
<div class="form-group">
<input placeholder="First Name" class="form-control" type="text" id="ContactViewModel_FirstName" name="ContactViewModel.FirstName" value="" />
</div>
<div class="form-group">
<input placeholder="Last Name" class="form-control" type="text" id="ContactViewModel_LastName" name="ContactViewModel.LastName" value="" />
</div>
<input type="submit" value="SUBMIT" class="btn btn-default" />
</form>
I have another Razor Page with a form backed by ShippingAddressViewModel.
<form method="post">
<div class="form-group">
<input placeholder="First Name" class="form-control" type="text" id="ShippingAddressViewModel_FirstName" name="ShippingAddressViewModel.FirstName" value="" />
</div>
<div class="form-group">
<input placeholder="Last Name" class="form-control" type="text" id="ShippingAddressViewModel_LastName" name="ShippingAddressViewModel.LastName" value="" />
</div>
<div class="form-group">
<input placeholder="Zip Code" class="form-control" type="text" id="ShippingAddressViewModel_ZipCode" name="ShippingAddressViewModel.ZipCode" value="" />
</div>
<input type="submit" value="SUBMIT" class="btn btn-default" />
</form>
I want to apply JS client validation to the FirstName and LastName. Since the name and id of the controls are different, I have to create two JavaScript methods to target the controls.
Is there a way in Razor Pages to back the page with different view models but the id and name are the same?
This problem is caused because you have an object. You can create two different variables in your PageModel.
[BindProperty]
public string FirstName { get; set; }
[BindProperty]
public string LastName { get; set; }
Now you your view can have the following code:
<input asp-for="FirstName" />
<input asp-for="LastName" />
This will have as a result to have the following html:
<input type="text" id="FirstName" name="FirstName" value="" />
<input type="text" id="LastName" name="LastName" value="" />
I hope it helps.

Why is my page displaying differently when running my site as opposed to on jsfiddle?

My html:
<section class="featured">
<div class="content-wrapper">
<h1>Hey! This is private!!!</h1>
<form action="" method="POST">
<fieldset>
<legend>(Not really) Opt In or Out</legend>
<input type="checkbox" id="selectTwitter" name="selectTwitter">I'm Twitterpated!</input>
<br />
<input type="checkbox" id="selectBingSearch" name="selectBingSearch">Bing Search</input>
<br />
<input type="checkbox" id="selectDuckDuckGoSearch" name="selectDuckDuckGoSearch">Duck Duck Go Search</input>
<br />
<input type="checkbox" id="selectYouTube" name="selectYouTube">You Tuber!</input>
<br />
<input type="checkbox" id="selectFlickr" name="selectFlickr">Flickr</input>
<br />
<input type="checkbox" id="selectWikipedia" name="selectWikipedia">Wikipedia</input>
<br />
<input type="checkbox" id="selectAmazon" name="selectAmazon">amazon</input>
<br />
<input type="checkbox" id="selectFlickr" name="selectFlickr">Fakebook</input>
<br />
<input type="submit" id="btnSubit" name="btnSubmit" value="Save Config changes</input><br />
</fieldset>
</form>
</div>
</section>
...displays as I would expect on jsfiddle (http://jsfiddle.net/CTyVk/)
But when I run my site, it appears like so:
What is the problem?
You didn't close <input>.. in <input type="submit" id="btnSubit" name="btnSubmit" value="Save Config changes</input><br />
You're missing a "> in your submit button code. Try:
<input type="submit" id="btnSubit" name="btnSubmit" value="Save Config changes"></input>

Jquery mobile radio button selection

I'm trying to implement a horizontal control group in jquery mobile which has radio buttons inside. I am trying to build the HTML string dynamically from code behind.
These radio buttons are in a for loop .
StringBuilder tileString = new StringBuilder(string.Empty);
foreach (Product product in productsInCart)
{
tileString.Append("<div><legend >Choose shipping type:</legend></div><br />");
tileString.Append("<input type='radio' name='radio-choice-1' id='radio-choice-1' onclick='findPaymentMethod()' value='In Flight' checked='checked' />");
tileString.Append("<label for='radio-choice-1'>In flight</label>");
tileString.Append("<input type='radio' name='radio-choice-1' id='radio-choice-2' value='On Arrival' onclick='findPaymentMethod()' />");
tileString.Append("<label for='radio-choice-2'>On Arrival</label>");
} `
As you can see there is a property on the first radio button.
checked='checked'
But when this is run in a for loop only the last radio element in the for loop gets checked. Can anyone suggest me whats wrong with the code ? Or am i missing something here ?
EDIT: HTML string that gets generated is added .
<div data-role='fieldcontain'>
<fieldset data-role='controlgroup' data-type='horizontal' id='6' data-mini='true'>
<div>
<legend >Choose shipping type:</legend>
</div>
<br />
<input type='radio' name='radio-choice-1' id='radio-choice-16' value='In Flight' checked='checked'/>
<label for='radio-choice-16'>In flight</label>
<input type='radio' name='radio-choice-1' id='radio-choice-26' value='On Arrival' />
<label for='radio-choice-26'>On Arrival</label>
</fieldset>
</div>`
`<div data-role='fieldcontain'>
<fieldset data-role='controlgroup' data-type='horizontal' id='1' data-mini='true'>
<div>
<legend >Choose shipping type:</legend>
</div>
<br />
<input type='radio' name='radio-choice-1' id='radio-choice-11' value='In Flight' checked='checked'/>
<label for='radio-choice-11'>In flight</label>
<input type='radio' name='radio-choice-1' id='radio-choice-21' value='On Arrival' />
<label for='radio-choice-21'>On Arrival</label>
</fieldset>
</div>
Only 1 radio element is getting checked within the fieldset
Here's an image of the desired effect.
Your name attribute (like id one) must be unique.
Working example: http://jsfiddle.net/Gajotres/SBxVc/
<div data-role='fieldcontain'>
<fieldset data-role='controlgroup' data-type='horizontal' id='6' data-mini='true'>
<div>
<legend >Choose shipping type:</legend>
</div>
<br />
<input type='radio' name='radio-choice-16' id='radio-choice-16' value='In Flight' checked='checked'/>
<label for='radio-choice-16'>In flight</label>
<input type='radio' name='radio-choice-26' id='radio-choice-26' value='On Arrival' />
<label for='radio-choice-26'>On Arrival</label>
</fieldset>
</div>
<div data-role='fieldcontain'>
<fieldset data-role='controlgroup' data-type='horizontal' id='1' data-mini='true'>
<div>
<legend >Choose shipping type:</legend>
</div>
<br />
<input type='radio' name='radio-choice-11' id='radio-choice-11' value='In Flight' checked='checked'/>
<label for='radio-choice-11'>In flight</label>
<input type='radio' name='radio-choice-21' id='radio-choice-21' value='On Arrival' />
<label for='radio-choice-21'>On Arrival</label>
</fieldset>
</div>

Zombie.js can't find button element on page

I'm trying to submit a form with zombie.js, and pressButton() is failing with "Error: No BUTTON 'xxx'", where xxx is the selector. Here is the app:
var Browser = require('zombie');
b = new Browser();
b.visit('https://www.example.com/login/', function() {
b.
fill('name', 'My Name').
fill('code', 'My Code').
pressButton('submit', function() {
console.log(b.html());
});
});
And here is the form:
<form method="post" class="login">
<p> <label for="name"> <span id="nameprompt">Your Name:</span> </label> </p>
<input name="name" id="name" value="" size="40" maxlength="40" />
<p> <label for="code"> <span id="codeprompt">Bar Code</span> </label> </p>
<input name="code" id="code" type="PASSWORD" value="" size="40" maxlength="40" />
<div class="formButtonArea">
<input type="image" src="/screens/pat_submit.gif" border="0" name="submit" value="submit" />
</div>
</form>
I've tried a bunch of selectors including things like ".formButtonArea input" in addition to the obvious "submit" with no success.
What am I doing wrong?
It seems like you need the input to be of type="submit" (or type="button" perhaps). According to https://groups.google.com/forum/?fromgroups=#!topic/zombie-js/8L0_Cpn8vVU, where the author comments on clickLink, it's likely that clickButton will do the same: find buttons and then fire click.
Hope it helps.

Resources