Here i am fetching the value from database and showing it in a input field
and
You can use simple form:
@using(Html.BeginForm("Update", "Shopping"))
{
<input type="text" id="ss" name="qty" value="@item.Quantity"/>
...
<input type="submit" value="Update" />
}
And add here attribute:
[HttpPost]
public ActionResult Update(string id, string productid, int qty, decimal unitrate)
When you want to pass new information to your application, you need to use POST form. In Razor you can use the following
View Code:
@* By default BeginForm use FormMethod.Post *@
@using(Html.BeginForm("Update")){
@Html.Hidden("id", Model.Id)
@Html.Hidden("productid", Model.ProductId)
@Html.TextBox("qty", Model.Quantity)
@Html.TextBox("unitrate", Model.UnitRate)
<input type="submit" value="Update" />
}
Controller's actions
[HttpGet]
public ActionResult Update(){
//[...] retrive your record object
return View(objRecord);
}
[HttpPost]
public ActionResult Update(string id, string productid, int qty, decimal unitrate)
{
if (ModelState.IsValid){
int _records = UpdatePrice(id,productid,qty,unitrate);
if (_records > 0){ {
return RedirectToAction("Index1", "Shopping");
}else{
ModelState.AddModelError("","Can Not Update");
}
}
return View("Index1");
}
Note that alternatively, if you want to use @Html.TextBoxFor(model => model.Quantity)
you can either have an input with the name (respectecting case) "Quantity"
or you can change your POST Update() to receive an object parameter, that would be the same type as your strictly typed view. Here's an example:
Model
public class Record {
public string Id { get; set; }
public string ProductId { get; set; }
public string Quantity { get; set; }
public decimal UnitRate { get; set; }
}
View
@using(Html.BeginForm("Update")){
@Html.HiddenFor(model => model.Id)
@Html.HiddenFor(model => model.ProductId)
@Html.TextBoxFor(model=> model.Quantity)
@Html.TextBoxFor(model => model.UnitRate)
<input type="submit" value="Update" />
}
Post Action
[HttpPost]
public ActionResult Update(Record rec){ //Alternatively you can also use FormCollection object as well
if(TryValidateModel(rec)){
//update code
}
return View("Index1");
}
I'll just try to answer the question but my examples very simple because I'm new at mvc. Hope this help somebody.
[HttpPost] ///This function is in my controller class
public ActionResult Delete(string txtDelete)
{
int _id = Convert.ToInt32(txtDelete); // put your code
}
This code is in my controller's cshtml
> @using (Html.BeginForm("Delete", "LibraryManagement"))
{
<button>Delete</button>
@Html.Label("Enter an ID number");
@Html.TextBox("txtDelete") }
Just make sure the textbox name and your controller's function input are the same name and type(string).This way, your function get the textbox input.
Try the following in your view to check the output from each. The first one updates when the view is called a second time. My controller uses the key ShowCreateButton and has the optional parameter _createAction with a default value - you can change this to your key/parameter
@Html.TextBox("_createAction", null, new { Value = (string)ViewBag.ShowCreateButton })
@Html.TextBox("_createAction", ViewBag.ShowCreateButton )
@ViewBag.ShowCreateButton
your link is generated when the page loads therefore it will always have the original value in it. You will need to set the link via javascript
You could also just wrap that in a form and have hidden fields for id
, productid
, and unitrate
Here's a sample for ya.
HTML
<input type="text" id="ss" value="1"/>
<br/>
<input type="submit" id="go" onClick="changeUrl()"/>
<br/>
<a id="imgUpdate" href="/someurl?quantity=1">click me</a>
JS
function changeUrl(){
var url = document.getElementById("imgUpdate").getAttribute('href');
var inputValue = document.getElementById('ss').value;
var currentQ = GiveMeTheQueryStringParameterValue("quantity",url);
url = url.replace("quantity=" + currentQ, "quantity=" + inputValue);
document.getElementById("imgUpdate").setAttribute('href',url)
}
function GiveMeTheQueryStringParameterValue(parameterName, input) {
parameterName = parameterName.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regex = new RegExp("[\\?&]" + parameterName + "=([^&#]*)");
var results = regex.exec(input);
if (results == null)
return "";
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
}
this could be cleaned up and expanded as you need it but the example works