Put value from view in viewBag - c#

I have a hidden field in my HTML view
<%:Html.HiddenFor(model=>model.ContactId) %>
I want to put this value in viewBag so that I can use it in controller. How to do that? Also, how will I access this in controller?

You don't have the notion of postback in ASP.NET MVC and the ViewBag is available to you to deliver data to the view, but not to transfer data back and forth between the view and the controller.
If your hidden input is in a form then upon posting the form the new value would be accessible via model.ContactId.
Example:
// Model
class TestModel
{
public string ContactId { get; set; }
}
//Controller
[HttpPost]
public ActionResult Edit(TestModel model)
{
string newId = model.ContactId;
}

Related

Force Save Sitefinity Widget MVC Controller Property

I am making a Sitefinity Widget using an MVC Controller. The widget shows and all that, no problem there. What I would like to do is save data for the widget in JSON form in a hidden property. I know how to make the property hidden via an attribute but I want to know how to POST data to my controller and set a property on the controller which will be saved in sf_control_properties table. Normally controller properties only get saved here when editing a widget's properties and clicking save in Admin UI. My control will have two modes (User view and Admin view). I am not making a custom designer via the edit modal popup. I am showing the design view while in page design mode on the widget itself. I have a save button that will do a POST to the controller on the backend. I want it so set a property and then persist it to the sf_control_properties table like a normal edit->modal->Save would do. I know I could connect directly to SQL and write the value but is there a better way to force a Widget Property to save in an MVC Controller than SQL brute force? Here is an example of my controller setup.
[ControllerToolboxItem(Name = "MyWidget", Title = "My Widget", SectionName = "Dashboard")]
public class MyWidgetController : Controller
{
public string Title { get; set; }
public string CssClass { get; set; }
public string CustomData {get;set;} //JSON string data
public ActionResult Index()
{
var viewModel = this.CustomData.FromJson<MyWidgetViewModel>();
return View(ViewModel);
}
[HttpPost]
public ActionResult Save(MyWidgetModel model)
{
this.CustomData = model.ToJson();
}
}
So basically when I do an ajax post to this controller I want to set this.CustomData = postModel.ToJson() and persist it to the sf_control_properties table.
I am doing this to avoid making a custom Dynamic Module with my own table, etc. When control loads in normal user view I will convert the this.CustomData property to C# class and use it in my razor view....

Update view after form submit

In my MVC controller I have two action methods.
The first one is Index method:
public IActionResult Index()
{
return PopulateViewModel();
}
The "PopulateViewModel" Action Method is used for updating of the view model and then showing these updated values on the Index view.
public IActionResult PopulateViewModel()
{
ViewModel viewModel = new ViewModel()
{
//updating values in the view model
//the values are received when the form in the view is submitted
};
return View("Index", viewModel);
}
The problem that I have is that on my Index view the updated values are not shown immediately after submitting the form in the view. When I submit the form I must then once again refresh the page to see the updated values.
What could be the reason for such behavior and how can I correct that?
You misunderstand the conceptual notion. The index is supposed to represent the initial page state. Other actions within the controller will modify the output by rendering the page with the adjusted model. Or handling server side model binding, but the concept is fundamentally achieving the same result.
Your controller logic should be within the following constraints.
public class SampleController : Controller
{
public IActionResult Index() => new View("...", ...);
public IActionResult SubmitSample(string location)
{
var model = service.GetLabLocations(location);
return View("...", model);
}
}
The index is simulating a GET request, returning the initial page in the required state. The form portion, should POST data, outlined in the SubmitSample portion of the code. This will change the state of the page, but the server will need to render with those changes. So the page will load with the attached model, for you to display.
This would represent Razor more than likely on the server side.
#if(Model != null)
foreach(var sample in Model)
{
// Markup, with the data
}

Pass value from URL into View ASP.NET MVC

I'm trying to pass the id value of the url into a textboxfor of a view like the picture below,and i have no idea how to do it.
here's the image
update, the problem is that value of id is pass from another view by action link like this
#Html.ActionLink("Chose Car Type for Car", "Addcar", "Car", new { id = item.Ctid }, null)|
so what i want is to pass an attribute from two different views belong to 2 different controller and different action, how can i do that, i just need to make the attribute from one view appear in other under any type like viewbag or anything.
when you enter your URL ,you are trying to call a Controller . so you should catch the parameter as argument of the controller method , and then pass it to view with something like viewbag
in your car controller:
public ActionResult AddCar(int Id)
{
ViewBag.Id= Id;
return View();
}
in your view :
#{int id = ViewBag.Id; }<br />
#Html.TextBox("TextboxName",#id)
One simple example in your context-
public class HomeController : Controller
{
public string Index(string id, string name)
{
return "ID =" + id+"<br /> Name="+name;
}
}
else while returning view, you can do something like-
return View("Act", new { yourQueryString= "itsValueHere" });
And after that you can-
Textboxfor is binding data with Model. So you have to bind view with a model.
make AddCart action with id paramter, after that you can see when you pass the value that will appear in id parameter. After that load the model or make the model with the id and properties and pass to the view.
In the View
#Html.TextBox("TextboxName",ViewBag.YourProperty)
In the Controller
ViewBag.YourProperty= 'test';
You should pass the ID through the model defined for this view.
Another way is to use Html.TextBox() and provide the value manually using #Request.RequestContext.RouteData.Values["id"]

How to safely pass modelinfo between different actions in MVC?

I've got a certain action in my controller.
I've created a view for it and linked a model in it... now I want to pass some info from a property from this model onto another action safely... Only ways I know are to pass it with #Html.ActionLink /with a hidden field in a form.
But these aren't secure at all as far as I know... so what other way is there to do this ?
You can store it in a session variable:
To store a property of the model in the first action:
public ViewActionResult SomeAction(SomeModel model)
{
Session["remember"] = model.someProperty;
return View();
}
To retrieve it in another action:
public ViewResult SomeOtherAction()
{
var rememberedValue = Session["remember"];
return View();
}

Change model property in post request asp.net mvc

I have one problem.
This is short example.
This is model.
public class MyModel
{
string Title{get;set;}
}
In view I write
#Html.TextBoxFor(model => model.Title)
This is controller.
public ActionResult EditNews(int id)
{
var model = new MyModel;
MyModel.Title = "SomeTitle"
return View("News/Edit", model);
}
//for post
[HttpPost]
public ActionResult EditNews(MyModel model)
{
//There is problem.When I do postback and
// change Title in this place,Title doesn't change in view textbox
//Only when I reload page it change.
model.Title = "NEWTITLE"
return View("News/Edit", model);
}
It won't change because by default (many think this is a bug) MVC will ignore the changes you make to the model in a HttpPost when you're returning the same View. Instead, it looks in the ModelState for the value that was originally served to the view.
In order to prevent this, you need to clear the ModelState, which you can do at the top of your HttpPost by doing:
ModelState.Clear();

Categories