我有ViewBag.count的局部视图,该视图在ShoppingCartController中定义。问题在于,仅当您在ShoppingCart View上时,ViewBag才会显示。我希望在所有视图上都可以看到ViewBag。我该如何解决?我目前正在像这样渲染部分:
private void checkPermissions(int fineLocationPermission) {
if (fineLocationPermission != PackageManager.PERMISSION_GRANTED) { //If we don't have any permissions yet
// TODO: Consider calling
//We have to request permissions and in case the user refuse the permission we show an explanation of why he needs the permission
//The following method returns true in case the user reject the permission
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
PERMISSION_REQUEST_CODE);
} else
mLocationPermissionGranted=true;
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_CODE:
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//If the user accepts the dialog box then we get the last location and show button
mLocationPermissionGranted=true;
} else {
mLocationPermissionGranted=false;
// permission denied, boo! Disable the
// functionality that depends on this permission.
Toast.makeText(this, "permission denied", Toast.LENGTH_LONG).show();
}
}
}
public void onLocationChanged(final Location location) {
String msg = "Updated location: " +
Double.toString(location.getLatitude()) + ", " +
Double.toString(location.getLongitude());
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
final LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
//Show button to locate current position
mMap.setOnMyLocationButtonClickListener(new GoogleMap.OnMyLocationButtonClickListener() {
@Override
public boolean onMyLocationButtonClick() {
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
CameraUpdateFactory.newLatLngZoom(latLng,12);
//mMap.setMaxZoomPreference(12);
return false;
}
});
// Add a marker our current position
LatLng CurrentPosition = new LatLng(location.getLatitude(), location.getLongitude());
mMap.addMarker(new MarkerOptions().position(CurrentPosition).title("Here you are!"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(CurrentPosition));
}
@Override
public void onMapReady(GoogleMap googleMap) {
//Create the map
mMap = googleMap;
//Set the type of the map: HYBRID, NORMAL, SATELLITE or TERRAIN. In our case TERRAIN type
mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
//Set the zoom
mMap.setMaxZoomPreference(12);
//Set the markers
MarkerOptions markerOptions= new MarkerOptions();
//Checking permissions with the permissions we have included in the manifiest
checkPermissions(permissionCheckFineLocation);
if (mLocationPermissionGranted) {
try {
mMap.setMyLocationEnabled(true);
//Last location task
Task<Location> locationResult = mFusedLocationClient.getLastLocation();
locationResult.addOnSuccessListener(this, new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
//Got last known location. In some rare situations this can be null
if (location != null) {
//Logic to handle location object
onLocationChanged(location);
}
}
});
} catch (SecurityException e) {
Log.e("error", e.getMessage());
}
}
}
名为_ShoppingCart的局部视图:
@Html.Partial("_ShoppingCart", new List<bytme.Models.ShoppingCartModel>())
答案 0 :(得分:1)
您应该创建一个单独的action方法,该方法返回呈现页面的购物车部分所需的HTML标记,并使用Html.Action方法将其包含在您的所有视图中。
您还可以使用from selenium import webdriver
from bs4 import BeautifulSoup as bs
import time
options = webdriver.ChromeOptions()
#install chrome if none and download chromedriver and add path to it
driver = webdriver.Chrome(executable_path="D:/Python/chromedriver", options=options)
driver.get("https://pcpartpicker.com/products/video-card/")
time.sleep(2)
soup = bs(driver.page_source,'lxml')
name = soup.find('tbody', {"id":"category_content"})
for i in name:
print(i.find('a').text)
属性修饰该操作方法,以使用户无法通过请求URL ChildActionOnly
直接访问此操作方法。
/ShoppingCart/Cart
并在部分视图([ChildActionOnly]
public ActionResult Cart()
{
ViewBag.ItemCount = 2; // replace hard coded value with your actual value
return PartialView();
}
)中,可以编写页面购物车部分所需的HTML代码。
~/Views/Shared/Cart.cshtml
在这里,我们使用ViewBag将项目计数数值从action方法传递到局部视图。但是您可以使用视图模型,并使用强类型视图方法将数据从操作方法传递到部分视图(这是我的首选方法)。
现在,在其他要呈现购物车HTML的视图/布局文件中,您可以调用<span class="mycart">
Total items in cart @ViewBag.ItemCount
</span>
方法
Html.Action
当剃刀执行视图时,它将看到此Html.Action方法,该方法将被执行,并且该方法的输出(通过action方法生成的HTML标记)将包含在为当前视图生成的最终输出中
我正在使用<div>
@Html.Action("Cart","ShoppingCart")
</div>
<h1>Welcome to my site</h1>
方法,因此它不会尝试执行布局代码。 (人们犯了这个错误,并无限次调用了Cart操作方法。
对于Asp.Net Core项目
如果要在asp.net核心项目中执行相同的操作,则可以使用View组件来获得相同的结果。
创建一个视图组件以呈现购物车。
PartialView
在public class CartViewComponent : ViewComponent
{
public IViewComponentResult Invoke(string name)
{
var totalItemCount = 3;
return View(totalItemCount);
}
}
目录中为此名称为Default.cshtml
的视图组件创建一个剃须刀视图,您可以在其中包含剃须刀代码/ HTML标记以呈现所需的HTML。在此示例中,我使用一种强类型方法,其中我的视图被严格地类型化为~/Views/Shared/Components/Cart
类型,并且在调用int
方法时从Invoke
方法传递了一个int值。
View
现在,您可以通过调用@model int
<span>
Total items : @Model
</span>
方法在其他视图/布局文件中调用此视图组件。
Component.InvokeAsync