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>
Related
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>
}
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>
}
How do I use MVC3 editor templates for lists with add and delete?
I have an object:
public class Policy
{
public List<PolicyLine> PolicyLines = new List<PolicyLine>();
}
public class PolicyLine
{
public PolicyLine(bool isPositive, string policyText)
{
IsPositive = isPositive;
PolicyText = policyText;
}
public bool IsPositive { get; set; }
public string PolicyText { get; set; }
}
I have an editorTemplate: in Views\Shared\EditorTemplates\Policy.cshtml and Views\Shared\EditorTemplates\PolicyLine.cshmtml and I'm wondering how to enable users to add and delete PolicyLines from the Policy?
For the DELETE, just add the following line to the PolicyLine.cshtml and add a Delete Action to your Controller to perform the delete.
#Html.ActionLink("Delete", "Delete", new { id = #Model.PolicyID })
The ADD is a bit trickier, you could add button to you Policy.cshtml and then call some javascript to insert some html on the fly.
OR
You could have the button display a new page to capture the new policyline and then return to the original page with the new line added.
I got this to work for me:
Here is my Views/Policy/Index.cshtml
#using (Html.BeginForm("Submit", "Policy")) {
<fieldset>
#Html.EditorForModel()
</fieldset>
}
Here is my Views/Shared/EditorTemplates/Policy.cshtml
#model Policy
<br />
<label for="IsPositive">Is positive?</label>
#Html.CheckBox("IsPositive")
<input type="text" name="PolicyText" />
<input type="submit" value="Add to Policy" title="SubmitFromReferalPolicy" />
#Html.EditorFor(a => a.PolicyLines)
Here is my Views/Shared/EditorTemplates/PolicyLine.cshtml
#model PolicyLine
<br />
#this.Model.ToString()
#Html.ActionLink("Delete", "DeleteLine/" + Model.Identifier.ToString())
Here is my Policy.cs
public class Policy
{
public string Id { get; set; }
public List<PolicyLine> PolicyLines = new List<PolicyLine>();
public override string ToString()
{
return PolicyFormatter.FormatPolicy(this);
}
}
Here is my PolicyLine.cs
public class PolicyLine
{
public bool IsPositive { get; set; }
public string PolicyText { get; set; }
public Guid Identifier { get; set; }
public override string ToString()
{
return PolicyFormatter.FormatPolicyLine(this);
}
}
Here is my add method from PolicyController.cs
[HttpPost]
public ActionResult Submit(PolicyLine submitted)
{
Policy saveMe = Policy.GetPolicyFromUserName(UserName);
submitted.Identifier = Guid.NewGuid();
saveMe.PolicyLines.Add(submitted);
Store.Write(saveMe);
return RedirectToAction("Index");
}
Here is my delete method from PolicyController.cs
public ActionResult DeleteLine(Guid identifier)
{
Policy saveMe = Policy.GetPolicyFromUserName(UserName);
PolicyLine removeMe = saveMe.PolicyLines.Find(p => p.Identifier == identifier);
saveMe.PolicyLines.Remove(removeMe);
Store.Write(saveMe);
return RedirectToAction("Index");
}
I have a number of child ViewModel classes which inherit from an base ViewModel class.
I pass my child ViewModel into my View, which then passes itself into a partial view.
The main view takes the child type, but the partial view takes the Parent type.
Everything displays correctly when I manually populate the properties.
However, when I Submit the form, my controller action only has properties for the Child class - none of the base class properties are completed?
e.g.
public abstract class BaseDetails
{
public string Name { get; set; }
public BaseDetails()
{ }
public BaseDetails(string name)
{
Name = name;
}
}
public class LocalDetails:BaseDetails
{
public int Visits { get; set; }
public LocalDetails()
{ }
public LocalDetails(int visits, string name)
:base(name)
{
Visits = visits;
}
}
With the View as simple as:
#using (Html.BeginForm())
{
#Html.TextBoxFor(m => m.Visits)
<br />
#Html.Partial("Name", Model)
<input id="Submit1" type="submit" value="submit" />
}
The partial view has a single textbox on it.
In IE: ViewSource shows the form tag is around both tetxboxes.
However, when I submit to the controller method:
[HttpPost]
public ActionResult EditLocal (LocalDetails model)
model.Visits is correctly populated, but model.Name is null?
Any ideas?
I added parameterless constructors to the classes because I got an exception upon submission if I didn't.
Unable to reproduce. Works fine for me. Notice that I am not using any constructors with my view models because the model binder wouldn't be able to call them and that all the properties must to have public getters and setters.
Model:
public abstract class BaseDetails
{
public string Name { get; set; }
}
public class LocalDetails : BaseDetails
{
public int Visits { get; set; }
}
Controller:
public class HomeController : Controller
{
public ActionResult Index()
{
return View(new LocalDetails());
}
[HttpPost]
public ActionResult Index(LocalDetails model)
{
return View(model);
}
}
View (~/Views/Home/Index.cshtml):
#model LocalDetails
#using (Html.BeginForm())
{
#Html.TextBoxFor(m => m.Visits)
<br />
#Html.Partial("Name", Model)
<input type="submit" value="submit" />
}
Partial (~/Views/Home/Name.cshtml):
#model BaseDetails
#Html.TextBoxFor(x => x.Name)
When I submit the form inside the POST Index action both model properties Name and Visits are correctly bound with values from the form.
i have a model SiteMapModel that have a object VirtualFolderModel inside him.
public class SiteMapModel
{
public SiteMapModel(DataRow data)
{
SMF_ID = Convert.ToInt32(data["SMF_ID"]);
SMF_VF_ID = Convert.ToInt32(data["SMF_VF_ID"]);
VirtualFolder = new VirtualFolderModel(data);
}
public VirtualFolderModel VirtualFolder;
public int SMF_ID { get; set; }
public int SMF_VF_ID { get; set; }
}
public class VirtualFolderModel
{
public VirtualFolderModel(DataRow data)
{
VF_ID = Convert.ToInt32(data["VF_ID"]);
}
public int VF_ID { get; set; }
}
in my controller i pass the model to a view.
public ActionResult Edit(int id)
{
SiteMapData smd = new SiteMapData();
SiteMapModel smm = new SiteMapModel(smd.GetFolderData((int)id, 15));
return View(smm);
}
how to use it in my view?
<div>
<span class="editor-label">
#Html.Label("Title")
</span>
#Html.TextBox("SMF_Name")
#Html.ValidationMessage("SMF_Name")
<span class="editor-label">
#Html.Label("VF_ID")
</span>
#Html.TextBox("VF_ID")
#Html.ValidationMessage("VF_ID")
<input type="submit" value="Save" />
</div>
the #Html.TextBox("VF_ID") don't work
At the top of your view add this:
#ModelType SitemapModel
Edit: For C# please use:
#model SitemapModel
Doing that will simply tell your view what kind of model is given at runtime. In this case, it's an object of type SitemapModel.
In your view you can reference to it to model.SMF_ID