How do i chnage submit button text inside mvc3 on condition? - asp.net-mvc

I have mvc 3 application in which i'm having one input form on Index.cshtml view. also having one webgrid which is having edit,delete button
depending upon these action links i need to change my submit button text. how can i achieve this inside homecontroller.cs ? using only one view for all edit,insert.
checking useraction inside homecontroller.cs
public ActionResult Index(string userAction)
{
if (userAction == "Edit" )
{
}
if (userAction == "Delete" )
{
}
}
View code.
<p>
<input type="submit" value="Insert" />
</p>
On webgrid having link for edit , delete
on that condition i need to change submit button text.
#if (grid != null)
{
#grid.GetHtml(
tableStyle: "grid",
headerStyle: "head",
alternatingRowStyle: "alt",
columns: grid.Columns(
grid.Column("", header: null, format: #<text>#Html.ActionLink("Edit", "Index", new { uid = (int)item.id, userAction = "Edit" })
#Html.ActionLink("Delete", "Index", new { uid = (int)item.id, userAction="Delete" }, new { #class = "Delete" })</text>),
}

You can store the userAction in ViewData or ViewBag and access it from the View.
public ActionResult Index(string userAction)
{
ViewBag.UserAction = userAction;
}
<input type="submit" value="#(ViewBag.UserAction == "X" ? "Y" : "Z")" />

Related

How can pass actionlink parameter to controller MVC 5?

How can I pass NextPrevious value to controller?
Now NextPrevious is getting always null
[HttpPost]
public ActionResult PremiumUserRegistration(PartnersVM partnersVM, string NextPrevious)
{
if (NextPrevious != null)
{
}
}
View And .js file,
$('.btnActionNext').click(function () {
$(this).closest('form')[0].submit();
});
#Html.ActionLink("Next >>", "PremiumUserRegistration", new { controller = "UserRegister" }, new { NextPrevious = "value", #class = "btnActionNext" , onclick = "return false;" })
Please help me...
Add a hidden:
<input type="hidden" name="Action" />
Make sure it's on your form. Next, on your click handler, do:
$('.btnActionNext').click(function () {
$("#Action").val("NEXT");
$(this).closest('form')[0].submit();
});
In your post action, add string Action:
[HttpPost]
public ActionResult PremiumUserRegistration(string Action, PartnersVM partnersVM)
{
if (Action == "NEXT")
{
}
}
If you are submitting a form, then add it as a hidden field
#Html.Hidden("NextPrevious", value)
Otherwise, you entered the "NextPrevious" value in the wrong spot in the ActionLink method. It should be within routeValues, not htmlAttributes.
The correct way would be:
#Html.ActionLink("Next >>", "PremiumUserRegistration", new { controller = "UserRegister", NextPrevious = "value" }, new { #class = "btnActionNext", onclick = "return false;" })

MVC How to change a buttons text on submit

I'm trying to create a simple MVC 4 app with a few buttons that when clicked adds one to their text value. The problem is when I use < input > buttons I can't change the text value when I submit the form and when I use < button > then there is nothing passed to the action.
I have a simple SQL Database that the controller hits which is where the initial ViewData values are coming from and how it's storing the buttons current value.
Also I'm currently trying to do this with multiple forms but if it is possible with one form then that would be ideal.
#using (Ajax.BeginForm("AddOne", "Home", null, new AjaxOptions { UpdateTargetId = "btn-tc-top"}, new { #class = "rectangle-top" }))
{
<input type="submit" class="button" name="button" id="btn-tc-top" value="#ViewData["ct-t"]"/>
}
#using (Ajax.BeginForm("AddOne", "Home", null, new AjaxOptions { UpdateTargetId = "btn-tc-bottom"}, new { #class = "rectangle-bottom" }))
{
<button type="submit" class="button" name="button" id="btn-tc-bottom">#ViewData["ct-b"]</button>
}
You could subscribe to the OnSuccess handler:
#using (Ajax.BeginForm("AddOne", "Home", new { id = "btn-tc-top" }, new AjaxOptions { OnSuccess = "addSuccess" }, new { #class = "rectangle-top" }))
{
<input id="btn-tc-top" class="button" type="submit" name="button" value="#ViewData["ct-t"]" />
}
#using (Ajax.BeginForm("AddOne", "Home", new { id = "btn-tc-bottom" }, new AjaxOptions { OnSuccess = "addSuccess" }, new { #class = "rectangle-bottom" }))
{
<input id="btn-tc-bottom" class="button" type="submit" name="button" value="#ViewData["ct-b"]" />
}
which could be defined like this:
function addSuccess(result) {
$('#' + result.id).val(result.text);
}
and the controller:
public class HomeController : Controller
{
public ActionResult Index()
{
ViewData["ct-t"] = "1";
ViewData["ct-b"] = "2";
return View();
}
[HttpPost]
public ActionResult AddOne(string id, int button)
{
return Json(new { id = id, text = button + 1 });
}
}

The model item passed into the dictionary is of type 'requires a model item of type 'System.Collections.Generic.IEnumerable`

I have created a web application in mvc3 and created two partial views
one having controls like dropdownlist.
second having webgrid which shows data from database.
partialview1.cshtml
#model Mapping.Models.SecurityIdentifierMapping
#using (Html.BeginForm("Mapping", "Home"))
{
#Html.DropDownList("SecurityID", Model.PricingSecurityID, "-- Select SecurityID --")
<br />
#Html.DropDownList("CUSIPID", Model.PricingSecurityID, "-- Select CUSIPID --")
<br />
<button type="submit">Map</button>
}
partialview2.cshtml
#model IEnumerable<Mapping.Models.SecurityIdentifierMapping>
#{
ViewBag.Title = "Mapping";
WebGrid grid = null;
if (Model.Count() > 0 ){
grid = new WebGrid(source: Model,
defaultSort: "Id",
canPage: true,
canSort: true,
rowsPerPage:20);
}
}
<h2>Mapping</h2>
#if (grid != null)
{
#grid.GetHtml(
tableStyle: "grid",
headerStyle: "head",
alternatingRowStyle: "alt",
columns: grid.Columns(
grid.Column("", header: null, format: #<text>#Html.ActionLink("Edit", "Edit", new { id = (int)item.id }) #Html.ActionLink("Delete", "Delete", new { id = (int)item.id })</text>),
grid.Column("PricingSecurityID"),
grid.Column("CUSIP")
)
)
}
<br />
<p>
#Html.ActionLink("Back", "Index")
</p>
in index.cshtml
<div>
#Html.Partial("_ControlsPartial",)
</div>
<div>
#Html.Partial("_WebGridPartial")
</div>
HomeController.cs
public ActionResult Index()
{
SecurityIdentifierMapping objModel = new SecurityIdentifierMapping();
objModel.PricingSecurityID = objRepository.GetPricingSecurityID();
objModel.CUSIP = objRepository.GetCUSIP();
return View(objModel);
}
How can i show webgrid and populate dropdown with same Index()??
getting error :(
what should be 2nd parameter inside #Html.Partial() so that both grid and control works fine on same page.?
You are passing a SecurityIdentifierMapping model to the Index view. Inside this Index view you are calling 2 partials:
#Html.Partial("_ControlsPartial")
and:
#Html.Partial("_WebGridPartial")
The first one works fine because it is strongly typed to SecurityIdentifierMapping but the second one (the one with the grid) doesn't work because it is strongly typed to IEnumerable<SecurityIdentifierMapping>. Thus the exception you are getting.
I would recommend you using a view model which will contain 2 properties: one simple SecurityIdentifierMapping that you could pass to the first partial and an IEnumerable<SecurityIdentifierMapping> property that you will pass to the second partial. It is the controller action that will fill this view model and pass it to the Index view:
Index.cshtml:
#model MyViewModel
<div>
#Html.Partial("_ControlsPartial", Model.SecurityIdentifier)
</div>
<div>
#Html.Partial("_WebGridPartial", Model.Identifiers)
</div>

How to show two Partials view data on Index.cshtml MVC3?

I have created a web application in mvc3 and created two partial views
one having controls two dropdownlist.
second having webgrid which shows data from database.
partialview1.cshtml
#model Mapping.Models.SecurityIdentifierMapping
#using (Html.BeginForm("Mapping", "Home"))
{
#Html.DropDownList("SecurityID", Model.PricingSecurityID, "-- Select SecurityID --")
<br />
#Html.DropDownList("CUSIPID", Model.PricingSecurityID, "-- Select CUSIPID --")
<br />
<button type="submit">Map</button>
}
partialview2.cshtml
#model IEnumerable<Mapping.Models.SecurityIdentifierMapping>
#{
ViewBag.Title = "Mapping";
WebGrid grid = null;
if (Model.Count() > 0 ){
grid = new WebGrid(source: Model,
defaultSort: "Id",
canPage: true,
canSort: true,
rowsPerPage:20);
}
}
<h2>Mapping</h2>
#if (grid != null)
{
#grid.GetHtml(
tableStyle: "grid",
headerStyle: "head",
alternatingRowStyle: "alt",
columns: grid.Columns(
grid.Column("", header: null, format: #<text>#Html.ActionLink("Edit", "Edit", new { id = (int)item.id }) #Html.ActionLink("Delete", "Delete", new { id = (int)item.id })</text>),
grid.Column("PricingSecurityID"),
grid.Column("CUSIP")
)
)
}
<br />
<p>
#Html.ActionLink("Back", "Index")
</p>
in index.cshtml
<div>
#Html.Partial("_ControlsPartial",)
</div>
<div>
#Html.Partial("_WebGridPartial")
</div>
inside Indexcontroller.cs in Index()
public ActionResult Index()
{
//FOR POPULATE DROPDOWN
//SecurityIdentifierMapping objModel = new SecurityIdentifierMapping();
//objModel.PricingSecurityID = objRepository.GetPricingSecurityID();
//objModel.CUSIP = objRepository.GetCUSIP();
//return View(objModel);
//FOR DISPLAY DATA IN WEBGRID
return View(dbContext.SecurityIdentifierMappings);
}
here problem is webgrid partial view is having#model IEnumerable<Mapping.Models.SecurityIdentifierMapping>
and
controlpartilview is having #model Mapping.Models.SecurityIdentifierMapping
so HOW CAN I CREATE A VIEWMODEL.cs A NEW CLASS WHICH WILL HAVE BOTH MODELS AND HOW CAN I WRITE IT IN INDEX(() SO THAT THAT METHOD WILL POPULATE DROPDOWN ALSO AND SHOW DATA IN WEBGRID ALSO ?
Why not just create a custom View Model class that contains two properties:
public class SecurityIdentifierMappingViewModel
{
public IEnumerable<SecurityIdentifierMapping> MappingList {get; set; }
public SecurityIdentifierMapping Mapping {get; set; }
}
Then you can pass this custom view model to the Index view and the corresponding property as the view model of each of the partials
EDIT
Your Index action would then look something like this:
public ActionResult Index()
{
// single mapping
var mapping = new SecurityIdentifierMapping();
maping.PricingSecurityID = objRepository.GetPricingSecurityID();
mapping.CUSIP = objRepository.GetCUSIP();
var viewModel = new SecurityIdentifierMappingViewModel
{
Mapping = mapping,
MappingList = dbContext.SecurityIdentifierMappings.ToList()
};
return View(viewModel);
}
And in Index.cshtml:
#model SecurityIdentifierMappingViewModel
<div>
#Html.Partial("_ControlsPartial", Model.Mapping)
</div>
<div>
#Html.Partial("_WebGridPartial", Model.MappingList)
</div>

Change value of grid item

I have table:
#using (Html.BeginForm()) {
#Html.ValidationSummary(true)
<fieldset>
<legend>Add</legend>
<br />
#{
var grid = new WebGrid(ViewBag.produkty,null, "names", 5);
}
#grid.GetHtml(
tableStyle: "grid",
headerStyle: "head",
alternatingRowStyle: "alt",
columns: grid.Columns(
grid.Column("name"),
grid.Column("value"),
grid.Column(header: "Add", format: (item) =>
new HtmlString(
Html.TextBoxFor(model => model.add).ToString())),
grid.Column( header: "Ok", format: (item) =>
new HtmlString(
Html.ActionLink("OK", "add_method", new { ID_name = item.ID_name }).ToString()))
)
)
</fieldset>
}
Controller:
public ActionResult use()
{
var nam = (from d in baza.Names
select new { d.ID_name, d.name, d.value}).ToList();
ViewBag.names= nam;
return View();
}
public ActionResult add_method(int ID_name, useModel use)
{
Use us = new Use();
var dat = DateTime.Today;
us.value = use.add;
us.ID_Name= ID_name;
us.data = dat;
baza.Zuzycies.InsertOnSubmit(us);
baza.SubmitChanges();
return RedirectToAction("use", "Product");
}
Model:
public class useModel
{
public int ID_name{ get; set; }
public decimal value{get;set;}
public string date { get; set; }
}
So, I have list of product on page. And I want to add a value (amount of product) into TextBox and press a ActionLink "OK" next to the textbox. How can I get amount of product in add_method? Or how insert submit button next to every one product (instead ActionLink "OK"), then is enought make use POST method...
You can use a grid componet with built-in edits functions (like the telerik Grid).
I think it's better to use ajax not reagular post request for your scenario.
Or you can do that ajax calls to the server with jquery, just send the parameters to the controller.

Resources