When the user clicks the submit button on a form I want to return a success / failure message on the form show me the demo
You could use a view model:
public class MyViewModel
{
public string Message { get; set; }
}
and then have a controller:
public class HomeController: Controller
{
public ActionResult Index()
{
var model = new MyViewModel();
return View(model);
}
[HttpPost]
public ActionResult Index(FormCollection fc)
{
var model = new MyViewModel
{
Message = "some message"
};
return View(model);
}
}
and a view:
#model MyViewModel
#if (!string.IsNullOrEmpty(Model.Message))
{
<div>#Model.Message</div>
}
#using (Html.BeginForm())
{
<button type="submit">OK</button>
}
Related
I've tried to follow a few examples from here and a couple other resources to just create a very simple member in my viewmodel and display it as a dropdown list on my view with a dropdownlistfor() helper. I can't seem to wrap my head around it and it's not working with what I'm trying.
here's my viewmodel:
public class Car
{
public int CarId { get; set; }
public string Name { get; set; }
}
public class MyViewModel
{
public IEnumerable<Car> Cars = new List<Car> {
new Car {
CarId = 1,
Name = "Volvo"
},
new Car {
CarId = 2,
Name = "Subaru"
}
};
public int MyCarId { get; set; }
}
and here is my view:
#Html.DropDownListFor(m => m.MyCarId, new SelectList(Model.Cars, "CarId", "Name"))
and here is my controller:
public ActionResult MyView()
{
return View();
}
You need to make sure you send the Model to your View:
public ActionResult Index()
{
var myViewModel = new MyViewModel()
return View(myViewModel);
}
And in your View you need to make sure you're defining your Model:
#model namespace.MyViewModel
You example works fine, I think you forgot to send MyViewModel in POST Action.
[HttpPost]
public ActionResult Index(MyViewModel model)
{
return View(model);
}
I am getting the following exception:
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 34: <div>
Line 35: #Html.LabelFor(m => m.accountStatus)
Line 36: (HERE) #Html.DropDownListFor(m => m.accountStatus, Model.accStat)
Line 37: </div>
Line 38: </fieldset>
Following is the model that I am using.
I have created a List property in my model. And I am referencing it in my Html.DropDownListFor helper.
But I am getting exception.
public class OperatorModel
{
[Required]
[Display(Name = "Account ID")]
public string accountID { get; set; }
[Required]
[Display(Name = "Account Name")]
public string accountName { get; set; }
[Required]
[Display(Name = "Password")]
public string password { get; set; }
public List<SelectListItem> accStat
{
get
{
List<SelectListItem> lst = new List<SelectListItem>();
lst.Add(new SelectListItem { Text="ACTIVE", Value="0", Selected=true });
lst.Add(new SelectListItem { Text="DEACTIVATED", Value="1" });
return lst;
}
}
EDIT
Following is my controller code:
public class AdministratorController : Controller
{
//
// GET: /Administrator/
public ActionResult Index()
{
return View();
}
public ActionResult Create()
{
return View();
}
public ActionResult Read()
{
return View();
}
public ActionResult Update()
{
return View();
}
public ActionResult Disable()
{
return View();
}
public ActionResult Enable()
{
return View();
}
}
You have to pass the OperatorModel to the view from your action:
for instance:
return View(new OperatorModel());
Hi im using RenderAction helper and during rendering i have this error:
Error executing child request for
handler
'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'.
Controller code:
public ActionResult GetSearcher()
{
SearcherLines sl = new SearcherLines()
{
RegionList = db.Region.ToList(),
CategoryList = db.Category.ToList(),
SearchValue = null
};
return PartialView(sl);
}
ViewModel
public class SearcherLines
{
public List<Region> RegionList { get; set; }
public List<Category> CategoryList { get; set; }
public string SearchValue { get; set; }
}
partialView
#model IEnumerable<MasterProject.JobForYou.v3.Models.ViewModels.SearcherLines>
#foreach (var i in Model)
{
<p>#i.CategoryList</p><br />
}
Please help.
Try setting OutputCache attribute on controller action
This is my View
#
using (Html.BeginForm("Display", "Home", #FormMethod.Post))
{
<div>Name:<a>#ViewBag.st</a><br /></div>
<a></a>
<div>City:<a>America</a></div>
<br />
<input type="submit" value="Submit" />
<br />
}
THis is model class
public class User
{
private string m_name = string.Empty;
public string Name
{
get
{
return m_name;
}
set
{
m_name = value;
}
}
}
This is my controller class
[HttpGet]
public ActionResult Viewdetails()
{
User ur=new User();
ur.Name = "Danny";
ViewBag.st = ur.Name;
return View();
}
[HttpPost, ActionName("Viewdetails")]
public ActionResult Test()
{
return View();
}
[HttpPost]
public ActionResult Display()
{
return View();
}
}
Now how to display Danny in my view in my Viewdetails.cshtml instead of using
#Viewbag
You could use an input field so that when the form is submitted its value is sent to the Display action. Also let's clean a little bit your code and get rid of ViewBag.
So you have a model which is fine:
public class User
{
public string Name { get; set; }
}
Then you could have a controller with 2 actions - one that populates the model and passes it to the Viewdetails.cshtml view and another action which is invoked when the form is submitted and renders the Display.cshtml view:
public class HomeController: Controller
{
[HttpGet]
public ActionResult Viewdetails()
{
User ur = new User();
ur.Name = "SravanKumar";
return View(ur);
}
[HttpPost]
public ActionResult Display(User ur)
{
return View(ur);
}
}
and then inside your Viewdetails.cshtml view you would use the model to render an input field instead of a link:
#model User
#using (Html.BeginForm("Display", "Home", FormMethod.Post))
{
<div>Name: #Html.EditorFor(x => x.Name)</div>
<div>City: Secunderabad</div>
<br />
<input type="submit" value="Submit" />
}
and inside your Display.cshtml view:
#model User
<div>Thanks for choosing the name: #Model.Name</div>
How can I enable and disable a #html.textbox in controller in mvc3?
My textbox code:
#Html.TextBox("txtIden1")
After click button, how can I disable or enable the Textbox in controller.
I wrote button click event code in controller, like below
#using(Html.BeginForm())
{
#Html.TextBox("txtCustomerFilter");
}
<button name="button" value="Submit">Save</button>
<button name="button" value="cancel">Cancel</button>
Controller:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Customer(string button,
customer customerInsert, FormCollection formvalues)
{
if(button == "Submit")
{
//Code
}
else
{
//Code
}
return View();
}
I would recommend you using a view model:
public class CustomerViewModel
{
public string Button { get; set; }
public string Filter { get; set; }
public bool Disabled { get; set; }
... some other properties that will represent the customer
details
}
and then:
public ActionResult Customer()
{
var model = new CustomerViewModel();
return View(model);
}
[HttpPost]
public ActionResult Customer(CustomerViewModel model)
{
if(model.Button == "Submit")
{
model.Disabled = true;
//Code
}
else
{
//Code
}
return View(model);
}
and then have a strongly typed view:
#model CustomerViewModel
#using (Html.BeginForm())
{
#Html.TextBoxFor(
x => x.Filter,
Model.Disabled ? new { #readonly = readonly } : null
)
<button name="button" value="Submit">Save</button>
<button name="button" value="cancel">Cancel</button>
}