I am using asp.net mvc application.
In my view page i have textbox like below code:
#Html.TextBoxFor(m => m.Id, new { #class = "form-control", #readonly = "readonly" })
Also, in form submit ajax post,
$('#btnSubmit').click(function(e)
{
$.ajax({
url: '/button/Button',
type: 'POST',
data: {Id:1},
});
});
My controller:
MasterM empcode = new MasterM();
[HttpGet]
public ActionResult Button()
{
var empcode = new MasterM();
return View(empcode);
}
[HttpPost]
public ActionResult Button(MasterM model)
{
ModelState.Clear();
if (!ModelState.IsValid)
{
return View(model);
}
return View(model);
}
after this form submit the value of the text box should change to new value (1). How can i achieve this in controller section itself.
I know in ajax success we can manually update the element value but i want to achieve this in controller section and update model with new values.
When you are Posting your are sending only a Single Id and not the Entire Model ,
So basically this
public ActionResult Button(MasterM model) will be empty
and since you are returning back the empty set the text box will not be updated.
There are 2 things you can do here :
1) Modify the Receiving type to
Public ActionResult Button(int Id)
{
MasterM model = new MasterM();
model.Id = Id;
if (!ModelState.IsValid)
{
return View(model);
}
return View(model);
}
2) If you want to retain Model as your Param in Controller
you need to serialize the Data in Jquery and post it .
Related
I am using FormMethod.post and for retrieving value on page and then post it.
index_shift.cshtml:
#using (Html.BeginForm("Index_shift", "Scm_Mod_Sug", FormMethod.Post))
{
<div>
#Html.DropDownList("shift_details", ViewBag.shift_details as SelectList, new { Class = "form-control selectpicker show-tick" })
</div>
}
In controller Scm_Mod_Sug
public ActionResult Index_shift()
{
ViewBag.shift_details = new SelectList(Getshift_details(), "ShiftVal", "ShiftVal");
return View();
}
After posting value to it want to return value to same view (index_shift.cshtml) and bind value on that page but I get an error
There is no ViewData item of type 'IEnumerable' that has the key 'shift_details'
Code:
[HttpPost]
public ActionResult Index_shift(FormCollection form)
{
shift_details = form["shift_details"];
ViewBag.Rval0 = values[0].Split('~');
return view()
}
Any idea how to achieve it would be appreciated.
Check this line:
ViewBag.shift_details =new SelectList(Getshift_details(), "ShiftVal", "ShiftVal");
You are writing value two twice. Change it to this:
ViewBag.shift_details =new SelectList(Getshift_details(), "ShiftId", "ShiftVal");
You must provide Id to the SelectList.
Razor code like:
#Html.TextBoxFor(Model => Model.Name, new { #Value = student.t_Name })
and I using .NET MVC's model validation in Controller,
if (ModelState.IsValid)
{
return RedirectToAction("B");
}
else
{
return View(); // when validation failed
}
My situation is I have a edit function, for example:
original data:
birthday: 1992-05-26
after edited:
birthday: 1992-05-32
after I submit this to Controller and make model validation, it will validate fail, and return to previous view(the view before form submit),
I want it shows
birthday:1992-05-32
instead of
birthday:1992-05-26
You should set ViewModel values that come to your controller like this:
public ActionResult YourControllerMethod(YourViewModel model)
{
if (ModelState.IsValid)
{
return RedirectToAction("B");
}
else
{
ViewData.Model = model; //where model is your controller model
return View(); // when validation failed
}
}
You would need to pass the current posted model instance back to view when returning back View something like:
public ActionResult YourAction(SomeModel model)
{
if (ModelState.IsValid)
{
return RedirectToAction("B");
}
else
{
return View(model);
}
}
I have a simple model I am using for a search page to do some validation:
public class Search {
[Required]
[DisplayName("Tag Number")]
[RegularExpression("([1-9][0-9]*)", ErrorMessage = "Tag must be a number")]
public int HouseTag { get; set; }
i then have a simple view with a textbox and a submit button:
#model Search
#{
Layout = "~/_Layout.cshtml";
}
#using (Html.BeginForm("Search", "Inquiry", FormMethod.Get)){
#Html.LabelFor(m =>m.HouseTag)
#Html.TextBoxFor(m=>m.HouseTag, new { type = "Search", autofocus = "true", style = "width: 200px", #maxlength = "6" })
<input type="submit" value="Search" id="submit"/>
my controller is expecting a parameter of an id:
[HttpGet]
public ActionResult Search(int id){
ViewBag.Tag = id;
return View();
}
when i execute it with a number i get a null value being passed to the controller, causing things to blow up. I am using the model to control some of the properties of the search box for validation. I used to just have #Html.TextBox and it returned fine, but now that ive added the model, it doesnlt return anything.
You can set your parameter to a type of Search and then access the property in your action
[HttpGet]
public ActionResult Search(Search model){
ViewBag.Tag = model.HouseTag;
return View();
}
If it were me I'd make this a HttpPost or create a seperate action for this form so I wouldn't see the HouseTag text in the URL..
#using (Html.BeginForm("Search", "Inquiry", FormMethod.Post))
{
#Html.LabelFor(m => m.HouseTag)
#Html.TextBoxFor(m => m.HouseTag, new { type = "Search", autofocus = "true", style = "width: 200px", #maxlength = "6" })
<input type="submit" value="Search" id="submit" />
}
[HttpPost]
public ActionResult Search(Search model){
ViewBag.Tag = model.HouseTag;
return View();
}
You are expecting a parameter named id and you are passing HouseTag as the name of that parameter you should rename id to houseTag inside the Search method.
There's a couple of things going on here. First you are going to want to split your Get and Post actions. Also forms are only used in conjunction with POST's. You also don't need to name your action or controller unless you are sending the post to a different controller or action then the GET.
This is the get. It renders the form on the page. You don't need to put [HttpGet] on there, it is the default.
public ActionResult Search()
{
return View();
}
The following is going to post the form back to the server. the model binder will wire up the html form fields with your view model. since you have validators on the view model, you'll want to check that the model state is valid and re-show the view with the associated errors. You will need to add an #Html.ValidationMessageFor(...) into your view so that you actually see those errors.
[HttpPost]
public ActionResult Inquiry(Search search)
{
if (!ModelState.IsValid)
{
return View(search);
}
//so something with your posted model.
}
I have a weird problem with #Html.ActionLink: it doesn't do anything. It doesn't go to the controller or load the view. I have a project with a menu. From the menu I want to redirect to another view. When I use #Html.ActionLink("Avioane", "Index", "Avioane", null, null) it doesn't go to controller.
public class AvioaneController : Controller
{
// [HttpGet]
public ActionResult Index()
{
//var db = new Curse_AerieneEntities();
//List<Avioane> avs = db.Avioane.Where(z => z.Disponibil).ToList();
//return View(avs);
return View();
}
};
When I use #Ajax.ActionLink("Avioane", "Index", "Avioane", new AjaxOptions{HttpMethod = "GET"}) and set [HTTPGet] on the controller, it goes to the view but doesn't load the view into the page.
What am I doing wrong?
I have a orchard project, where I have created a module in MVC. I want to pass the id of particular user to controller using #Html.ActionLink but it not calling controller. Here is my code:
In view:
#Html.ActionLink("100111", "AddToCart", "ShoppingCart", new { id = 101 }, null)
//also tried,
#Html.ActionLink("102829", "AddToCart", "ShoppingCart", new { id = 1, area = "OnlineShopping" },null)
In Controller:
[HttpPost]
public ActionResult AddToCart(int id)
{
_shoppingCart.Add(id, 1);
return RedirectToAction("Index");
}
[Themed]
public ActionResult Index()
{
// Create a new shape using the "New" property of IOrchardServices.
var shape = _services.New.ShoppingCart();
// Return a ShapeResult
return new ShapeResult(this, shape);
}
It is not calling the action method because of [HttpPost] and clicking on anchor tag actually does a Get. So try remove the [HttpPost] attribute decoration from your AddToCart action.
[HttpPost]//<--Remove this
public ActionResult AddToCart(int id)
{
_shoppingCart.Add(id, 1);
return RedirectToAction("Index");
}
Found this on ASP.NET MVC ActionLink and post method
You can't use an ActionLink because that just renders an anchor tag.
You can use a JQuery AJAX post, see
http://docs.jquery.com/Ajax/jQuery.post or just call the form's submit
method with or without JQuery (which would be non-AJAX), perhaps in
the onclick event of whatever control takes your fancy.